template: extract key and value from a JsonField inside a form

hi all
I have a model with the parameter test=models.JSONField field, and I pass it to template with view.py as a form.
in the template I would like read the value of a key:
for example the Jsonfield is {“title”:“test”,“description”:“testDescription”}
it is passed to template with the patientForm

in the template I would like add

{{ patientForm.test.title }}

but nothing is printed. In the lines

{% render_field visitaForm.test class="form-control" %}
I can read the complete line {"title".....} described before

Please post the actual model, form, and the view showing what you’re passing into the template context.

form is

class PatientVisitaForm(forms.ModelForm):
    class Meta:
        model=models.Visita
        fields=['title','commenti','data','diagnosi','consigliTerapeutici','score']
        widgets={'data': forms.DateInput(
                    format=('%Y-%m-%d'),
                    attrs={'class': 'form-control', 
                        'placeholder': 'data di nascita',
                        'type': 'date'
                        }),
        }

models is

class Visita(models.Model):
    patient = models.ForeignKey(Patient,on_delete=models.CASCADE,default=None)
    score = models.JSONField(default=dict,null=True,blank=True,)
    created = models.DateTimeField(auto_now_add=True)
    @property
    def get_patient_id(self):
        return self.patient_id
    class Meta:
        ordering = ['-data']  
        get_latest_by = 'data'  

    def __str__(self):
        return self.title

views is

def add_patient_visita_view(request,pk):
    print("----------------",pk, request)
    patient=models.Patient.objects.get(id=pk)
    patientForm=forms.PatientForm(instance=patient)  
    scoreList = [{'id': i.id, 'title':i.get_title} for i in models.EsameObiettivo.objects.all()]
    if request.method=='GET':
        if request.GET.get('scoreList_id'):
            print('request.get',request.GET,request.GET.get('scoreList_id'))
            scoreQuestion = models.EsameObiettivo.objects.get(id=request.GET.get('scoreList_id'))
            mydict={'scoreQuestion': scoreQuestion.score}
            return HttpResponse(json.dumps(mydict), content_type='application/json')
        else:
            visita=models.Visita.objects.all()
            print("visita vecchie len",len(visita))
            visita=models.Visita.objects.all().filter(patient_id=patient.get_patient_id).latest('created')
            print("visita DATA CREAZIONE ",visita.created)
            if visita:
                print("VISITA TITOLO", visita.title)
                visitaForm=forms.PatientVisitaForm(instance=visita)
            else:
                visitaForm=forms.PatientVisitaForm()
            scoreQuestion = {}
            mydict={'patientForm':patientForm,'visitaForm':visitaForm,'scoreList':scoreList,'scoreQuestion': scoreQuestion}   

    elif request.method=='POST':

        print("REQUEST POST",request.POST)
        
        visitaForm=forms.PatientVisitaForm(request.POST)
        if visitaForm.is_valid():
            print("scrittura 1")
            visitaNew = visitaForm.save(commit=False)
            visitaNew.id = None
            visitaNew.patient_id = pk
            visitaNew.doctor_id = 2
            actualScoreList = models.EsameObiettivo.objects.get(id=request.POST.get('scoreList'))
            visitaNew.score = {'titolo': actualScoreList.title, 'descrizione': actualScoreList.descrizione, 'score': actualScoreList.score}
            
            scoreAnswer = [i for i in request.POST if 'answer' in i]
            for i in range(len(scoreAnswer)):
                visitaNew.score['score'][i]['risposta'] = request.POST.get('answer'+str(i+1))
            visitaNew.save()
            print("visitaNew.score",visitaNew.score)
            print("nuovo ID",visitaNew.id)
            mydict={'patientForm':patientForm,'visitaForm':visitaForm,'scoreList':scoreList}
        else:
            print("scrittura 3")
            print('visitaForm.errors',visitaForm.errors)
            scoreQuestion = {}
            mydict={'patientForm':patientForm,'visitaForm':visitaForm,'scoreList':scoreList,'scoreQuestion': scoreQuestion}  

    return render(request,'hospital/admin2patient/admin_add_patient_visita.html',context=mydict)

The issue here is that visitaForm.score is a reference to a Form field, not the Model field. The Form field has a reference to the Model field for rendering purposes, but the reference to visitaForm.test itself is not the models.JSONField.

If you’re looking to access the model itself, it would be in the visitaForm.instance element.

So the reference to the score field is visitaForm.instance.score, and so on.