Danjgo:CharField不支持的查找'case_exact'或不允许在字段上加入

时间:2018-12-10 07:13:45

标签: python django django-views

这是我的服务器上遇到的GET请求。

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

借助此视图,我正在从GET请求中获取值。

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

现在下面是我要检查具有给定角色名称的对象是否存在的功能。如果存在,那么我要更新值(如果它是新角色),那么我要创建新的testPersona对象。

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')

以下是我遇到的错误。

    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

请您解释一下为什么我会收到此错误,以及如何删除此错误?预先感谢。

2 个答案:

答案 0 :(得分:1)

更改此行

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

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

答案 1 :(得分:0)

最后一整天在这个问题上,我都能知道我在哪里做错了。

在TestPersona模型中,有一个'name'字段,它是对TestPersonaName进行建模的外键,这意味着TestPersonaName的对象将分配给TestPersona的'name'字段。因此答案将是:

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]

    if(persona_name is None and persona_key is None and persona_key_label is None and persona_key_value is None):

    # Since no paramteres are persent in GET request(i.e Nothing is filled in form), then we will simply render the blank form.
        return render(request, 'dashboard/test_page.html')

 #  Below is the function for updating testPersona . 

    elif TestPersonaName.objects.filter(name__iexact=persona_name).exists():
        testPersonaName_obj = TestPersonaName.objects.filter(name__iexact=persona_name) #Fetching an object from TestPersonaName model where name = persona_name
        testpersonaSet = TestPersona.objects.filter(name=testPersonaName_obj) #Passing testPersonaName_obj to 'name' field of TestPersona model because name is foreign key.
        for testpersona in testPersonaSet: #testPersonaSet is a QuerySet
            if testpersona.key == persona_key: #Checking whether persona with given key is already present or not

         # If TestPersona object with given name and give key is already present then we will update the details instead of making a new object.
                testpersona.label= persona_key_label 
                testpersona.value = persona_key_value
                testpersona.save()
                return render(request,'dashboard/test_page.html')    

#If persona with new name is detected then saving new objects of TestPersonaName and TestPersona object.
    testPersonaName = TestPersonaName(name=persona_name)
    testPersonaName.save() 
    testpersona(name=testPersonaName,key=persona_key,label=persona_key_label
                ,value=persona_key_value)
    testpersona.save()
    return render(request,'dashboard/test_page.html')