带有答案的测验模型的Formset

时间:2014-03-03 10:37:44

标签: django django-models django-forms

我的测验应用程序中有以下模型:

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

我需要呈现可能看起来像的形式:

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

所以,每个问题总会有4个答案。 我应该如何编码这种模式的表单?有什么建议吗?

1 个答案:

答案 0 :(得分:0)

我认为您正在寻找https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#inline-formsets

我创建了一个测验应用,可能会为您节省一些时间

https://github.com/tomwalker/django_quiz