2 回答
TA贡献1802条经验 获得超4个赞
不要通过ID。这不是 Django ORM 的用途。只需循环浏览项目本身:
for project in Project.objects.all():
seventh_question_project = Seventhquestion.objects.all().filter(project=project).first() # <-- not project_id!
# or better:
# seventh_question_project = project.seventhquestion_set.first()
if seventh_question_project:
# you should always check, because you need to write fool-proof code
answer_seventh_five = seventh_question_project.seventh_five
...
但是通过关系,您可以更轻松地获取相关对象。因此,假设模型上的project字段是 a ,则相反的关系是:OneToOneFieldSeventhquestionseventhquestion
for project in Project.objects.all():
if hasattr(project, "seventhquestion"):
# still need to check, you never know
answer_seventh_five = project.seventhquestion.seventh_five
...
TA贡献2041条经验 获得超4个赞
这个怎么样?
seventh_question_project = Seventhquestion.objects.filter(project=project_id).first()
answer_seventh_five = seventh_question_project.seventh_five
添加回答
举报