我正在尝试生成一个查询,我在django shell中获得了预期的结果,但对于同一个查询,我收到的错误是该模型的属性不存在。
首先是shell:
>>> from dbaccess.models import *
>>> applicantObject = Applicant.objects.get(pk=5)
>>> vol = VolInterview.objects.get(applicant=applicantObject)
>>> vol
<VolInterview: Rajon>
来自views.py
from models import *
def addIntCandidate(request):
applicants = Applicant.objects.filter(applicationStatus="Pending")
interviews = Interview.objects.all()
message = []
if request.method == 'POST':
applicant = request.POST.get('applicant')
...
# the value of applicant at this point is 5
applicantObject = Applicant.objects.get(pk=applicant)
prevRejected = VolInterview.objects.get(applicant=applicantObject)
...
错误讯息:
type object 'VolInterview' has no attribute 'objects'
回溯:
E:\projects_directory\djangoprojects\kpr-admin-db\dbaccess\views.py in addIntCandidate
prevRejected = VolInterview.objects.get(applicant=applicantObject)
我做错了什么?
答案 0 :(得分:1)
您的视图中似乎已将VolInterview
替换为其他类。
要检查,您可以将print(VolInterview)
添加到您的视图和shell中,并检查两者是否得到相同的结果。
在Django中,通常使用Form
作为表单类的后缀,例如VolInterviewForm
,以便模型和表单名称不会发生冲突。