3 回答
TA贡献1993条经验 获得超5个赞
改变这一行
testpersona = TestPersona.objects.get(name__case_exact=persona_name)
到
testpersona = TestPersona.objects.get(name__name__case_exact=persona_name)
TA贡献1853条经验 获得超6个赞
确切和模型字段名称之间应该有2 个下划线。
假设您正在匹配名为persona_name 的字段
elif TestPersonaName.objects.filter(persona_name__iexact=persona_name).exists():
testpersona = TestPersona.objects.get(persona_name__iexact=persona_name)
iexact 无论大小写敏感,都匹配值。
TA贡献1858条经验 获得超8个赞
最后,在这个问题上花了一整天之后,我能够知道我在哪里做错了。
在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')
添加回答
举报