Django为什么内联formset验证失败?

时间:2014-11-19 13:26:48

标签: django validation formset

查看

def add_poll_checkbox(request):
new_poll = Poll.objects.create(author=request.user)
if request.POST:
    form = AddPoll(request.POST, instance=new_poll)

    if form.is_valid():
        poll = form.save(commit=False)
        poll.author = request.user
        poll.poll_type = 1

        formset = ChoiceFormSet(request.POST, instance=poll)
        if formset.is_valid():
            poll.save()
            formset.save()
            return HttpResponseRedirect('/admin/teacher/')
    else:
        formset = ChoiceFormSet(instance=new_poll)
else:
    form = AddPoll(instance=new_poll)
    formset = ChoiceFormSet(instance=new_poll)

return render_to_response('add_poll_checkbox.html', {'form': form, 'formset': formset},
                          context_instance=RequestContext(request))

表格

class AddPoll(forms.ModelForm):

class Meta:
    model = Poll

ChoiceFormSet = inlineformset_factory(Poll, Choice, extra=2, can_delete=False)

请告诉我该代码有什么问题?我无法保存该表格和表格。

0 个答案:

没有答案