django具有相同模型和外键的多个模型表单

时间:2013-09-12 05:21:04

标签: python django django-forms

我正在使用Django构建一个简单的问答应用程序。我的简化模型是:

class Question(models.Model):
    question_text = models.TextField('Question', max_length=256)

class AnswerChoice(models.Model):
    choice_text = models.CharField('Choice', max_length=32)
    question = models.ForeignKey(Question)
    is_correct = models.BooleanField(default=False)

上面两个模型(ModelFormQuestionForm)我有两个AnswerChoiceForm

现在,我在HTML页面上显示QuestionForm和4 AnswerChoiceForm,用于编辑问题并为问题添加4个答案选项。我想确保用户正好标记一个答案为"正确"。

我的观点功能是:

def edit_question(request):
    if request.method == 'POST':
        question_form = QuestionForm(request.POST)
        choice_forms = [AnswerChoiceForm(request.POST, prefix=str(i))
                        for i in xrange(4)]
        if all(c.is_valid() for c in choice_forms) and question_form.is_valid():
            choices = [c.save(commit=False) for c in choice_forms]
            question = question_form.save()
            for c in choices:
                c.question = question
                c.save()
            return HttpResponseRedirect(...) # show the question just added
     # ...

现在,我想验证中只有一个中的4个选项被标记为正确。我可以在上面的edit_question视图函数中进行此检查,但不知何故,这似乎有点"错误":我正在为视图函数添加核心逻辑,我并不完全满意。

有没有办法在我的QuestionAnswerChoice模型中或在模型表单的定义中进行此验证?

我没有在上面提供完整的最小代码,希望显示的代码量足够且不会太长。如果您需要查看更多代码,请询问我将编辑此帖子。

2 个答案:

答案 0 :(得分:2)

这里的问题是您没有在答案表单中使用formset。你应该:它们不仅比分别实例化四个表单更笨拙,而且它们有一个clean()方法,专门用于验证跨越子表单,而不是每个表单。像这样:

class AnswerFormSet(forms.models.BaseInlineFormSet):
     def clean(self):
        correct_count = sum([form.cleaned_data['is_correct'] for form in self.forms])
        if correct_count != 1:
            raise forms.ValidationError('Exactly one answer must be marked as correct')

在视图中你会这样做:

def edit_question(request):
    AnswerFormset = forms.models.inlineformset_factory(
         Question, Answer, formset=AnswerFormSet, extra=4, max_num=4)
    if request.method == 'POST':
        question = Question()
        question_form = QuestionForm(request.POST, instance=question)
        answer_formset = AnswerFormset(request.POST, instance=question)
        # Check these separately to avoid short-circuiting
        question_valid = question_form.is_valid()
        answer_valid = answer_formset.is_valid()
        if question_valid and answer_valid:
            question_form.save()
            # No need to add new question as it was already set as the instance above
            answer_formset.save()
        # etc

答案 1 :(得分:0)

一个选项如下:

class Question(models.Model):
    question_text = models.TextField('Question', max_length=256)
    def validate_answers(obj):
     if obj.answerchoice_set.filter(is_correct=True).count()==1
       #All well
       return True
     else:
       #delete question and answers if you wish or request for change
       return False

但是你应该记住,这将在保存所有内容后检查你的答案是否有效。如果您愿意,可以删除您的问题或答案。

def edit_question(request):
  #your usual code
  return (HttpResponseRedirect(...) if question.validate_answers() else  HttpResponseRedirect(...))