【发布时间】:2020-09-22 22:57:09
【问题描述】:
我正在构建 Django 测验应用程序,它将包含 3 种类型答案的问题:
- 单选答案;
- 多选答案;
- 文字回答;
我不确定我应该如何为这种模式设计 django 模型。
目前我的模型如下所示:
class Question(CoreModel, models.Model):
TYPES = (
(1, 'radio'),
(2, 'checkbox'),
(3, 'text'),
)
type = models.CharField(max_length=8, choices=TYPES, default='radio')
text = models.CharField(max_length=2048, null=False, blank=False)
class Choice(CoreModel, models.Model):
question = models.ForeignKey(Question, on_delete=models.DO_NOTHING)
text = models.CharField(max_length=2048, null=False, blank=False)
correct = models.BooleanField(default=False)
class Answer(CoreModel, models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.DO_NOTHING)
question = models.ForeignKey(Question, on_delete=models.DO_NOTHING)
choice = models.ForeignKey(Choice, on_delete=models.DO_NOTHING)
text = models.CharField(max_length=2048, null=False, blank=False)
created = models.DateTimeField(auto_now_add=True)
但它似乎无法按预期工作。 我真的不喜欢在我的 Answer 模型中同时使用“选择”和“文本”字段,但这就是我能想到的。
有什么想法或建议吗?也许我需要更多其他模型来进一步逻辑?
【问题讨论】:
标签: python django model-view-controller django-rest-framework