title: Django: ManagementForm data is missing ... Formset will not validate. Why?
body: <p>For some reason my formset will not validate, after I submit it.  Any ideas as to way this may be happening?</p>
  
  <h2>#models.py</h2>
  
  <pre><code>class Department(models.Model):
    department = models.CharField(verbose_name = "Department Name", max_length=100)
    description = models.TextField(verbose_name = "Description", blank=True, null=True)
    sp_description = models.TextField(verbose_name = "Description (Spanish)", blank=True, null=True)
    phone = PhoneNumberField()
    phone_ext = models.CharField(max_length=10, blank=True)
  
    #Relations
    provider = models.ForeignKey(Provider, blank=True, null=True)
    services_offered = models.ManyToManyField(ServiceType)
  
    def __unicode__(self):
      return self.department
  </code></pre>
  
  <h2>#view.py</h2>
  
  <pre><code>def display_step5(request):
      msg = ''
      email = request.session.get('email')
      provider_obj = retrieve_provider_instance(email)
      AddDepartmentFormSet = inlineformset_factory(Provider, Department, extra=0)
      if is_authenticated(request):
          AddDepartmentFormSet = inlineformset_factory(Provider, Department, extra=0)
          if request.method=='POST':
              if 'add_department' in request.POST:
                  cp = request.POST.copy()
                  cp['department-TOTAL_FORMS'] = int(cp['department-TOTAL_FORMS'])+ 1
                  new_department = AddDepartmentFormSet(cp, prefix='department', instance=provider_obj)
              elif 'submit' in request.POST:
                  formset = AddDepartmentFormSet(request.POST, instance=provider_obj)
                  instances = formset.save(commit=False)
                  for instance in instances:
                      instance.provider = provider_obj
                      instance.save()
                  return HttpResponse("Departments have been added")
          else:
              new_department = AddDepartmentFormSet(prefix='department', instance=provider_obj)
  
      else:
          return HttpResponseRedirect(reverse('health.register.views.display_auth'))
      return render_to_response('step5-test.html',
  {'department': new_department}, context_instance=RequestContext(request))
  </code></pre>
  
  <hr>
  
  <h1>step5-test.html</h1>
  
  <pre><code>&lt;form method='post' action='{{request.path}}'&gt;{% csrf_token %}
    &lt;fieldset&gt;
      &lt;legend&gt;Departments&lt;/legend&gt;
        {{ department.management_form }}
        {% for form in department.forms %}
          &lt;HR&gt; &lt;div&gt;&lt;table&gt;{{form.as_table}}&lt;/table&gt;&lt;/div&gt;
        {% endfor %}
       &lt;BR/&gt; 
       &lt;input title='Add a new department' type='submit' name='add_department' value='Add-Another-Department' /&gt;
      &lt;/fieldset&gt;
      &lt;BR/&gt;&lt;BR/&gt;
      &lt;input title='Submit this form' type='submit' name='submit' value='Submit' /&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p><strong>Follow up (solution):</strong></p>
  
  <p>Brant offered gave some insight into why the error was occurring. For my case, his insight helped me construct a hack that worked better for my situation. This hack seems applicable for a formsets that may have been modified in a certain way within the template.  In my case, my formset became complicated and required some ajax to make it more user friendly. </p>
  
  <p>The "retrieve_provider_instance(email)" was a dynamic way of retrieving the provider object based on various criteria set in the database (about 200 lines of code vomit). </p>
  
  <p>The following hack worked for me, after some extensive debugging. I solved it by adding this the following <em>hidden field</em> in my template's "for" loop                   </p>
  
  <pre><code>{% {for form in formset.forms %}
                       &lt;tr&gt;
                        &lt;td&gt;
                           {% if form.instance.pk %} &lt;!--- if instance already exists on db ---&gt;  
                           {{ form.DELETE }}
                           &lt;input type='hidden' name="department_set-{{ forloop.counter0 }}-id" id="id_department_set-{{ forloop.counter0 }}-id"  value={{ form.instance.pk }} /&gt;
                           {% endif %}
                        Name: &lt;br/&gt; {{ form.department}} &lt;br/&gt;
                        Phone Number: &lt;br/&gt; {{ form.phone }} &lt;br/&gt;
                        Phone Ext: &lt;br/&gt; {{ form.phone_ext }}&lt;/td&gt;
                        &lt;td&gt;Description: &lt;br/&gt; {{ form.description }}&lt;/td&gt;
                        &lt;td&gt;Description (Spanish): &lt;br/&gt; {{ form.sp_description }}&lt;/td&gt;
                        &lt;td&gt;Services Provided: &lt;br/&gt; {{ form.services_offered }}&lt;/td&gt;
                        &lt;td&gt;&lt;/td&gt;
                     &lt;/tr&gt;
                     {% endfor %} 
  </code></pre>
  
  <p>I accepted Mark's answer, because it was much more applicable for the specific question I was asking. Hopefully, this will help other users in the future who try to implement this example. Indeed, assigning the 'prefix' on submit is the correct way and makes the most sense.  </p>
  
  <p>Thank-you for all of your responses.</p>
  
  <p>Best Regards,
  Matt</p>
  
  <hr>
  
  <p><strong>Follow-up</strong>: I think I may have found part of my problem.  But, it is not solved yet.</p>
  
  <p>Here is some additional information that may help solve the problem. <em>Could incorrect POST data be messing this up?</em> And if so, how do I work around this?</p>
  
  <p>This is what happens after I submit the formset (in this case there is only one form):</p>
  
  <p><strong>Traceback is as follows:</strong></p>
  
  <blockquote>
    <p>File "/home/mhjohnson/webapps/resourcedb/lib/python2.6/django/forms/formsets.py" in _management_form
      68.                 raise ValidationError('ManagementForm data is missing or has been tampered with')</p>
  </blockquote>
  
  <p><strong>This is my POST data:</strong></p>
  
  <pre><code>department-0-phone_ext -----&gt; u'222'
  
  department-0-id -----&gt; u''
  
  department-0-services_offered -----&gt; u'2'
  
  department-0-phone -----&gt; u'222-222-2222'
  
  department-INITIAL_FORMS -----&gt; u'0'
  
  submit -----&gt; u'Submit'
  
  department-MAX_NUM_FORMS -----&gt; u''
  
  department-0-department -----&gt; u'Test 1'
  
  department-0-provider -----&gt; u''
  
  department-TOTAL_FORMS -----&gt; u'1'
  
  csrfmiddlewaretoken -----&gt; u'92a39322b16ed7e5e10dbd6c5ac64bf4'
  
  department-0-description -----&gt; u'Blah blah'
  
  department-0-sp_description -----&gt; u'....'
  </code></pre>
  
  <p><strong>Any ideas?</strong></p>