Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: Unsupported lookup 'case_exact' for CharField or join on the field not permitted

This is the GET request hitting on my server.

 HTTP GET /testPage/?persona_name=Aman&key_name=country&key_label=My+Country&key_value=India&Save=Submit 500

With the help of this view i am fetching the values from GET request.

def PersonaSave(request):
    persona_name = request.GET.get('persona_name',)
    persona_key = request.GET.get('key_name',)
    persona_key_value = request.GET.get('key_value',)
    persona_key_label = request.GET.get('key_label',)
    persona_submit = request.GET.get('Save',)
    return( persona_name , persona_key , persona_key_label , persona_key_value , persona_submit

Now following is the function where i am checking whether object with the given persona name exists or not. If it exists then i am updating the values if it is a new persona then i am making new testPersona object.

def TestPageView(request):
    x=PersonaSave(request)
    persona_name = x[0]
    persona_key = x[1]
    persona_key_label=x[2]
    persona_key_value=x[3]
    persona_submit=x[4]
    testPersonaName = TestPersonaName(name=persona_name)
    testPersonaName.save()

    if(persona_name is None and persona_key is None and persona_key_label is None and persona_key_value is None):
        return render(request, 'dashboard/test_page.html')

 # Below is the function for updating testPersona . 

    elif TestPersonaName.objects.filter(name__case_exact=persona_name).exists():
        testpersona = TestPersona.objects.get(name__case_exact=persona_name)
        if testpersona.key == persona_key:
            testpersona.label= persona_key_label
            testpersona.value = persona_key_value
            testpersona.save()




#If persona with new name is detected then saving a new testPersona object.
      testpersona=TestPersona(name=persona_name,key=persona_key,label=persona_key_label,value=persona_key_value)
        testpersona.save()

        return render(request,'dashboard/test_page.html')

Following is the error that i am getting.

    django.core.exceptions.FieldError: Unsupported lookup 'case_exact' for CharField or join on the field not permitted.

Below are TestPersona and TestPersonaName models.

    class TestPersonaName(models.Model):
        name = models.CharField(max_length=100,null=True)
        def __str__(self):
            return self.name

    class TestPersona(models.Model):
        name = models.ForeignKey('TestPersonaName',null=True)
        key  = models.CharField(max_length=200,null=True,blank=True)
        label = models.CharField(max_length=200,null=True,blank=True)
        value = models.CharField(max_length=200,null=True,blank=True)
        def __str__(self):
            return self.name + " " + self.key

May you please explain me why i am getting this error and how can i remove this error? Thanks in advance.

like image 282
Amandeep Singh Avatar asked Oct 20 '25 04:10

Amandeep Singh


2 Answers

change this line

 testpersona = TestPersona.objects.get(name__case_exact=persona_name)

to

 testpersona = TestPersona.objects.get(name__name__case_exact=persona_name)
like image 89
Exprator Avatar answered Oct 21 '25 16:10

Exprator


You should have 2 underscores between exact and model field name.

Let's say you are matching field called persona_name

elif TestPersonaName.objects.filter(persona_name__iexact=persona_name).exists():
    testpersona = TestPersona.objects.get(persona_name__iexact=persona_name)

iexact match the values regardless of case sensitiveness.

like image 40
Rarblack Avatar answered Oct 21 '25 16:10

Rarblack