views:

82

answers:

1

I have a models.py class as below

class Educational_Qualification(models.Model):
   user = models.ForeignKey(User)
   exam = models.CharField(max_length=40)
   pass_month = models.CharField(max_length=40)

I have a views.py as below

def create_qualification(request):
 QFormSet = modelformset_factory(Educational_Qualification, extra=3, exclude=("user",))
 if request.method == "POST":
  formset = QFormSet(request.POST, request.FILES)

  if formset.is_valid():
   formset.save()
   for form in formset.forms:
    if form.is_valid():
     quali= form.save(commit=False)
     quali.user = request.user
     quali.save()

    return HttpResponse("Saved")
  else:
   return HttpResponse("Snafu")
 else:
  formset = QFormSet()
 return render_to_response("register/edu.html", {"formset":formset}, context_instance=RequestContext(request)) 

When I submit the form, it throws up the validation Error. stating that ManagementForm data is missing or has been tampered with'

I have formset.management_form in my template.

What could be the issue?

+1  A: 

The error is not in your views or the models, but in the templates.

The right way to render the formset, is:

<form method="post" action="">
    <table>
        {{ formset }}
    </table>
</form>

or

<form method="post" action="">
    {{ formset.management_form }}
    <table>
        {% for form in formset.forms %}
            {{ form }}
        {% endfor %}
    </table>
</form>

I guess, you are looping over the forms in the templates without including the management form?

Lakshman Prasad