Reputation: 6511
I am trying to make a model for a "Question/Quiz" . A question can have as many choices (Charfields) as user wants , and there can be one or many correct answers (out of those options) . How should I define this model?
Question: Some long description.
So far I came up with this but I think it is a bad design:
class Question(models.Model):
description = models.CharField('Description', max_length=300)
correct_answer_id = models.UUIDField('answer_id', null=True)
options = models.ManyToManyField(Answer)
class Answer(models.Model):
"""
Answer's Model, which is used as the answer in Question Model
"""
text = models.CharField(max_length=128, verbose_name=u'Answer\'s text')
ans_id = models.UUIDField(default=uuid.uuid1())
Upvotes: 0
Views: 70
Reputation: 2233
I think you'd have a data model like: quiz has one-to-many questions, and questions have one-to-many answers (of which multiple can be correct).
So I'd do something like:
class Quiz(models.Model):
...
class Question(models.Model):
quiz = models.ForeignKey(Quiz)
...
class Answer(models.Model):
question = models.ForeignKey(Question)
text = ...
is_correct = models.BooleanField(default=False)
Then most of the details would be based on how you query to build the "survey"/ quiz and grade it.
Upvotes: 0
Reputation: 73470
As pointed out by Daniel, you should provide your answers with a correct-flag. Also, it seems more sensible to give them a fk to Question
as multiple questions fitting the same answer is odd. This way one answer always belongs to a single question:
class Question(models.Model):
description = models.CharField('Description', max_length=300)
def correct_answers(self):
return self.answer_set.filter(correct=True)
# ...
class Answer(models.Model):
text = models.CharField(max_length=128)
question = models.ForeignKey(Question)
correct = models.BooleanField('Correct', default=False)
Upvotes: 3