broinjc
broinjc

Reputation: 2699

Validating a formset

I am getting this error: ValidationError at /screen-many/ [u'ManagementForm data is missing or has been tampered with'] and I think it is due to the folling code in my view...

# e_pk_list is a list of id's that I got from POST
e_students = Student.objects.filter(pk__in=e_pk_list)
my_iterator = iter(e_students) # Each list item will correspond to a form.
SurveyFormset = formset_factory(SurveyForm, extra=len(e_students))

# Is this the tampering that I can't do??
SurveyFormset.form = staticmethod(curry(SurveyForm, item_iterator=my_iterator))

if request.method == 'POST':

    survey_formset = SurveyFormset(request.POST)

    if survey_formset.is_valid():
        for form in survey_formset:
            saved = form.save(commit=False)
            saved.surveyset = ss
            saved.save()
        return HttpResponseRedirect('/')
else:
    survey_formset = SurveyFormset()

Thanks

EDIT: I guess I should have mentioned that I already have a managementform in my template....

    <form action="" method="POST">{% csrf_token %}
        {{ survey_formset.management_form }}
        {% for form in survey_formset %}
            <div class="item">
              {% crispy form %}
            </div>
        {% endfor %}
        <input type="submit" value="Submit" class='button' />
    </form>

Upvotes: 0

Views: 264

Answers (2)

Akshar Raaj
Akshar Raaj

Reputation: 15211

A formset has many forms. Django keeps track of number of forms in formset using management form data. You should add management_form in the template too, which should be posted along with other POST data.

So, you should have:

<form method="POST" action=".">
    {{survey_formset.management_form}}
    {% comment %}Other form fields{% endcomment %}
</form>

Upvotes: 0

Priyank Patel
Priyank Patel

Reputation: 3535

Its seems that you didn't put management_form in your form .

Put this in your html form where your are displaying SurveyFormset

{{ SurveyFormset.management_form }}

Upvotes: 1

Related Questions