Arun Laxman
Arun Laxman

Reputation: 386

Design approach for Multiple choice quiz app with Multiple right answers

Hi I'm trying to implement a django quiz app. But can't figure out the approach i should use when trying to create a question with multiple right answers. ie, users should be able to mark multiple choices as the right answers.

This is what I've come up with so far.

class Question(models.Model):
    question = models.CharField(...)

class Choice(models.Model):
    question = models.ForeignKey("Question")
    choice = modelsCharField("Choice", max_length=50)

class Answer(models.Model):
    question = models.ForeignKey("Question")
    answers = models.ForeignKey("Choice")

Please guide me how to implement it the right way.

Upvotes: 0

Views: 216

Answers (1)

Kyle
Kyle

Reputation: 105

You don't need another model for answer. Just modify Choice model like this.

class Choice(models.Model):
    question = models.ForeignKey('Question')
    choice = models.CharField(...)
    is_answer = models.BooleanField(default=False) # or True.

And then, you can make some useful methods in Question.

class Question(models.Model):
    question = models.CharField(...)

    def check_answer(self, choice):
        return self.choice_set.filter(id=choice.id, is_answer=True).exists()

    def get_answers(self):
        return self.choice_set.filter(is_answer=True)

I recommend change your field name such as question in Question, choice in Choice. This can cause confusion.

Upvotes: 2

Related Questions