Reputation: 575
I have a modelformset that I want to save to a model.
In my templates, the following works perfectly (meaning that the form saves to the database when I hit submit):
{{ formset.management_form }}
{{ formset }}
Even this works great:
{{ formset.management_form }}
{% for row in formset %}
{{ row }}
{% endfor %}
But as soon as I want to style my formset (in a table) like so:
{{ formset.management_form }}
{% for row in formset %}
{{ row.field1 }}
{{ row.field2 }}
{{ row.field3 }}
{% endfor %}
I get [u'ManagementForm data is missing or has been tampered with'].
Fields 1 to 3 corresponds exactly to the model I want to save to. I really cannot figure this out!
My traceback shows this:
GET No GET data
POST Variable Value
form-0-field1 u'3'
form-0-field2 u'3'
form-0-field3 u'3'
Which are the same values I used in the first two examples (and which saved correctly).
Upvotes: 0
Views: 1858
Reputation: 575
I finally found the answer (in the docs) that coincidently explains this matter exactly as I asked my question. The third option must render {{ row.id }}, otherwise Django will create the validation error. It is up to the user to use 'style="display: none;"' in the inline html, but it must be rendered.
Here's the link: https://docs.djangoproject.com/en/1.7/topics/forms/modelforms/#using-the-formset-in-the-template
Tested and it works!
Upvotes: 1
Reputation: 5554
Your rendering of the form doesn't include the additional data which are required for the ManagementForm (form-TOTAL_FORMS
, form-INITIAL_FORMS
and form-MAX_NUM_FORMS
)
See: https://docs.djangoproject.com/en/1.7/topics/forms/formsets/#understanding-the-managementform
Upvotes: 1