Reputation: 3636
I want to pass a formset as an argument to a function and want to validate the formset. If the question is not correct somebody please correct the question.
This is my function:
def valid_check(*args):
valid = True
for arg in args:
if not arg.is_valid():
valid = False
return valid
valid = valid_check(form1,formset1,formset2)
if valid:
#do something
But this not working and showing this error:
[u'ManagementForm data is missing or has been tampered with']
Upvotes: 1
Views: 51
Reputation: 45575
Seems like your forgot to output management_form
s in your html template or to add the prefix
to the formsets:
formset1 = FormSet1(prefix='formset1')
formset2 = FormSet2(prefix='formset2')
And then in the template:
{{ formset1.management_form }}
{{ formset2.management_form }}
UPDATE: to the question asked in the comments:
Never use the if form.is_valid() and formset1.is_valid:
statement. If the form
is invalid then the formset
validation will not run. So your valid_check()
function is much more correct solution - it checks all passed forms/formsets.
But I prefer to use the python's built-in all()
function:
if all([form.is_valid(), formset1.is_valid(), formset2.is_valid()]):
In this case is_valid()
is called for each form/formset in the list construction and all()
returns True
only if all forms are valid.
Upvotes: 3