django - 为什么related_name 不适用于模型?
问题描述
有相互关联的问答表。每个问题都有一个答案。我需要检查 CompanyGeneralInfoAnswers 模型中的答案字段。
class CompanyGeneralInfoQuestion(models.Model):
rfi = models.ForeignKey(Rfis, models.DO_NOTHING)
question = models.CharField(max_length=512)
class Meta:
db_table = 'company_info__questions'
class CompanyGeneralInfoAnswers(models.Model):
question = models.OneToOneField('CompanyGeneralInfoQuestion', models.DO_NOTHING, related_name='answer_to_question')
answer = models.CharField(max_length=4096, blank=True, null=True)
class Meta:
db_table = 'company_info_answers'
为此,我提出了这个要求。
round = kwargs.get('rfiid')
exist_company_question = CompanyGeneralInfoQuestion.objects.filter(rfi=round)
if exist_company_question:
for q in exist_company_question:
print(q.answer_to_question.answer)
但是遇到了错误
RelatedObjectDoesNotExist: CompanyGeneralInfoQuestion has no answer_to_question.
解决方案
推荐阅读
- c# - 如何将页面名称显示为导航栏标题 iOS PageRenderer
- jenkins - 有没有办法在 Jenkins 作业参数中询问用户问题?
- c# - Azure 函数:system.private.corelib:执行函数时出现异常
- verilog - 在verilog中从PRBS生成器向错误检查模块提供输入
- javascript - 如何用 HTML 元素替换 jQuery 对象?
- javascript - 如何更新幻灯片数量(猫头鹰轮播)
- spring-mvc - 如何在我的 Spring MVC REST 控制器中接受多个多部分文件
- python - 从 MySQL(SQLAlchemy) 读取后需要帮助编写 bonobo XML writer 转换
- angular - Angular4 - 无法绑定到“vgHls”,因为它不是“视频”的已知属性。
- python - 如何使用 swig 将 c++ int** 返回值类型映射到 python