dease
dease

Reputation: 3076

Formset for quiz models with answers

I have following models in my quiz app:

class Question(models.Model):
    uuid = ShortUUIDField()
    content = models.CharField(max_length=32)
    author = models.CharField(max_length=32, blank=True, null=True, verbose_name='Author')
    def __unicode__(self):
        return self.content

class Answer(models.Model):
    question = models.ForeignKey(Question)
    content = models.CharField(max_length=32)
    valid = models.BooleanField(default=False)

    def __unicode__(self):
        return self.content

I need to render form which may look like:

---
Question
---
[input for answer 1] [ ] Valid?
[input for answer 2] [ ] Valid?
[input for answer 3] [ ] Valid?
[input for answer 4] [ ] Valid?

So, every question will always have 4 answers. How should I code form for such pattern? Any suggestions?

Upvotes: 0

Views: 389

Answers (1)

tomwalker
tomwalker

Reputation: 338

I think that you are looking for https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#inline-formsets

I have created a quiz app which might save you some time

https://github.com/tomwalker/django_quiz

Upvotes: 1

Related Questions