Django why inline formset validation fails?

133 views Asked by At

View

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))

Form

class AddPoll(forms.ModelForm):

class Meta:
    model = Poll

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

Please tell me what is wrong in that code? I can't save that form and formset.

0

There are 0 answers