16

I keep getting the error:

[u'ManagementForm data is missing or has been tampered with']

I can't figure out why either. Here is my view:

   def CreateWorkout(request):
    WorkoutInlineFormSet = inlineformset_factory(workout,exercise)
    if request.method == "POST" : 
        formset = WorkoutInlineFormSet(request.POST)

        if formset.is_valid(): 
            formset.save(); 
    else: 
        formset = WorkoutInlineFormSet()
    return render_to_response('submit.html',{'formset': formset},context_instance=RequestContext(request))

And here is my template:

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

        </table>
</form> 
</body>

I've read that you have to include the formset.management_form, and I have. I thought that would be an easy fix, but I haven't been able to figure out the problem.

iJade
  • 23,144
  • 56
  • 154
  • 243
Lilluda 5
  • 1,111
  • 3
  • 18
  • 38

3 Answers3

7

I have meet this problem.

The reason is there is NO something like form-TOTAL_FORMS, form-INITIAL_FORMS and form-MAX_NUM_FORMS) in your POST data.

You should use {{ formset.as_p }}, this will render the management_form data from the formset. If you want to make the custom formset rendering, you should not forget the management_form of the formset to let POST data be with the mangement_form data.

IKavanagh
  • 6,089
  • 11
  • 42
  • 47
ryanking
  • 309
  • 2
  • 6
6

When you use inline formset, you need to provide the instance that the objects relate to.

# First, fetch the instance from the db
workout = code_that_fetches_instance()

if request.method == "POST" : 
    formset = WorkoutInlineFormSet(request.POST, instance=workout)
    ...
else: 
    formset = WorkoutInlineFormSet(instance=workout)

See the example in the docs on using an inline formset in a view for more information.

If workout and exercise are your models, you should follow the python convention and rename them Workout and Exercise. Lowercase workout should be the instance that all the exercises in your formset are linked to.

Alasdair
  • 298,606
  • 55
  • 578
  • 516
  • Still the same error. The error is occurring during the declaration for formset underneath request.method == "POST" – Lilluda 5 Oct 13 '12 at 22:41
  • After making my change did you reload the code and make a fresh get request before submitting again? Please update your question with the html output when you make a get request with my suggested change. – Alasdair Oct 13 '12 at 22:44
  • You haven't included the html output that I asked for. I've updated the answer with a couple more suggestions. – Alasdair Oct 13 '12 at 23:06
  • You get the error message on the get request as well as when you submit the form? The full traceback might be helpful. I think you need to sort out the other issues I've mentioned. I can't help you any more tonight. Hope you get it working. – Alasdair Oct 13 '12 at 23:17
  • are workout and exercise instances or models? I have same issue. @Alasdair, what about a form for a new model instance with related objects? – christophe31 Feb 08 '16 at 10:19
  • @christophe31 this question is several years old, and never got a definitive answer, so I don't think it will help you much. I think the OP named the models `workout` and `excercise`, but it would be much clearer to use `Workout` and `Excercise`. Creating a new instance and related objects at the same time is a different issue. [This question](http://stackoverflow.com/questions/1113047/creating-a-model-and-related-models-with-inline-formsets) might be what you are looking for. If not, please ask a new question. – Alasdair Feb 08 '16 at 11:29
  • you're right, this one is not 3 years old at least… ^^, thanx for the tip and sorry for the joke. (I think i fixed my issue related to chaining `if form.is_valid and formset1.is_valid() and formset2.is_valid():` which probably postponed the content validation to rendering and caused a crash… but not sure yet as I didn't produced the bug myself) – christophe31 Feb 08 '16 at 14:43
1

Change this:

  formset = WorkoutInlineFormSet(request.POST)

to this:

 formset = WorkoutInlineFormSet(request.POST or None, request.FILES or None)
Pika Supports Ukraine
  • 3,612
  • 10
  • 26
  • 42