我正在构建 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 模型中同时使用“选择”和“文本”字段,但这就是我能想到的。
有什么想法或建议吗?也许我需要更多其他模型以获得进一步的逻辑?
最佳答案
我会在以下方面做一些事情:
class Quiz(CoreModel, models.Model):
name = models.CharField(max_length=2048, null=False, blank=False)
class Question(CoreModel, models.Model):
TYPES = (
(1, 'radio'),
(2, 'checkbox'),
(3, 'text'),
)
quiz = models.ForeignKey(Quiz)
type = models.CharField(max_length=8, choices=TYPES, default='radio')
prompt = models.CharField(max_length=2048, null=False, blank=False)
correct_free_text = models.CharField(max_length=2048, null=True, blank=True)
rank = models.SmallIntegerField(default=0)
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)
rank = models.SmallIntegerField(default=0)
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, null=True, on_delete=models.DO_NOTHING)
free_text = models.CharField(max_length=2048, null=True, blank=True)
created = models.DateTimeField(auto_now_add=True)
我会尽量将“可重用性”保持在最低限度。我的意思是不要尝试重复使用 Choice
或 Question
在多个 Quiz
es,它们是否包含相同的信息并不重要,让它们不同。希望这会有所帮助,如果您有任何其他问题,请告诉我。
关于python - 具有不同类型问题的 Django 测验应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64018749/