Danjgo:不支持对CharField或join的“case_exact”查找,不允许在字段上执行join

2024-09-29 23:28:23 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

在这个视图的帮助下,我从GET请求获取值。在

^{pr2}$

下面是我检查给定角色名的对象是否存在的函数。如果它存在,那么我正在更新值如果它是一个新的persona,那么我将创建新的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

你能解释一下为什么我会遇到这个错误,以及我怎样才能消除这个错误?提前谢谢。在


Tags: keynameselfnonetrueisvaluemodels
3条回答

在这个问题上花了一整天的时间之后,我终于知道我在哪里犯了错误。在

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

换行

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

^{pr2}$

精确字段名和模型字段名之间应该有2个下划线。在

假设您正在匹配名为persona_name的字段

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

iexact匹配值,而不考虑大小写敏感度。在

相关问题 更多 >

    热门问题