我想创建名为Question
的问题的基础模型,并通过任何其他问题类型扩展它:
class Question(models.Model):
questionnaire = models.ForeignKey(to='Questionnaire', on_delete=models.CASCADE, related_name='questions')
question = models.TextField()
description = models.TextField(null=True, blank=True)
media = models.FileField(upload_to='medias', blank=True, null=True)
is_required = models.BooleanField(default=False)
def __str__(self):
return f'{self.questionnaire.name} - {self.question}'
我想这样扩展它
class OptionalQuestion(Question):
multiple_choice = models.BooleanField(default=False)
additional_options = models.BooleanField(default=False)
# If multiple_choice is True, then max_selected_options and min_selected_options will be used
max_selected_options = models.IntegerField(null=True, blank=True)
min_selected_options = models.IntegerField(null=True, blank=True)
# If additional_options is True, then all_options and nothing_selected will be used
all_options = models.BooleanField(default=False, null=True, blank=True)
nothing_selected = models.BooleanField(default=False, null=True, blank=True)
def save(self, *args, **kwargs):
if not self.multiple_choice:
self.max_selected_options = None
self.min_selected_options = None
if not self.additional_options:
self.all_options = None
self.nothing_selected = None
if self.nothing_selected or self.all_options:
self.all_options = False
self.multiple_choice = False
super().save(*args, **kwargs)
这在第一次创建迁移时是很好的,但是下一次Django要我为'question_ptr'字段提供默认值时,我甚至不知道这个字段是什么
我试着让基类抽象化,但在迁移时Django显示了很多错误,比如:
question_app.TextAnswerQuestion.questionnaire: (fields.E305) Reverse query name for 'question_app.TextAnswerQuestion.questionnaire' clashes with reverse query name for 'question_app.DropDownQuestion.questionnaire'.
HINT: Add or change a related_name argument to the definition for 'question_app.TextAnswerQuestion.questionnaire' or 'question_app.DropDownQuestion.questionnaire'.
question_app.TextAnswerQuestion.questionnaire: (fields.E305) Reverse query name for 'question_app.TextAnswerQuestion.questionnaire' clashes with reverse query name for 'question_app.NumberAnswerQuestion.questionnaire'.
HINT: Add or change a related_name argument to the definition for 'question_app.TextAnswerQuestion.questionnaire' or 'question_app.NumberAnswerQuestion.questionnaire'.
question_app.TextAnswerQuestion.questionnaire: (fields.E305) Reverse query name for 'question_app.TextAnswerQuestion.questionnaire' clashes with reverse query name for 'question_app.OptionalQuestion.questionnaire'.
HINT: Add or change a related_name argument to the definition for 'question_app.TextAnswerQuestion.questionnaire' or 'question_app.OptionalQuestion.questionnaire'.
我不知道该怎么办
1条答案
按热度按时间7uzetpgm1#
首先,使问题模型抽象= True
当抽象基类中具有ForeignKey关系时,每个派生类都将具有此关系。因此,不能硬编码其related_name,因为所有派生类都将尝试在相关类上创建相同的访问器。
正如Django文档所建议的,你可以在Questionmodel中做一些类似的事情:
查看Django文档点击这里