title: Django Management Form is failing because 'form-TOTAL_FORMS' and 'form-INITIAL_FORMS' aren't correctly populated
body: <h2>Information:</h2>
  
  <p>I would like to create nested forms as is best described through the example provided at: </p>
  
  <p><a href="http://yergler.net/blog/2009/09/27/nested-formsets-with-django/" rel="nofollow">http://yergler.net/blog/2009/09/27/nested-formsets-with-django/</a></p>
  
  <p>The tutorial at this page seems to be pretty good &amp;&amp; it is attempting to accomplish the exact problem I am encountering.  </p>
  
  <p>There seems to be an issue with this implementation in the views.py file when there is no POST request data (I.e. we are performing the initial population from database).  </p>
  
  <p>The code can be seen at the URL provided above (if needed I can post some of the code, but I fear it will take away from the information provided here).  </p>
  
  <p>Here is the views.py code that is failing (in bold):</p>
  
  <pre><code>block = get_object_or_404(models.Block, id=block_id)
  
  if request.method == 'POST':
      formset = forms.BuildingFormset(request.POST, instance=block)
  
      if formset.is_valid():
          rooms = formset.save_all()
  
          return redirect('block_view', block_id=block.id)
  
  else:
      formset = forms.BuildingFormset(instance=block)  #This is the line that is throwing the ValidationError 
  </code></pre>
  
  <p>The Error Message I am getting is:</p>
  
  <pre><code>ValidationError at "urlName":
  [u'ManagementForm data is missing or has been tampered with']
  </code></pre>
  
  <p>I have dug deeper and it appears this failure is ocurring at line site-packages/django/forms/formsets.py</p>
  
  <p>The <code>is_valid()</code> check is failing because some of the managementform required data (form-TOTAL_FORMS, form-INITIAL_FORMS and form-MAX_NUM_FORMS) is invalid.  Here is the actual output of self.errors below:</p>
  
  <pre><code>{u'TOTAL_FORMS': [u'This field is required.'], u'INITIAL_FORMS': [u'This field is required.']}
  </code></pre>
  
  <h2>Code:</h2>
  
  <p>edit_building.html:</p>
  
  <blockquote>
    <p>{{ buildings.management_form }} </p>
    
    <p>{% for building in buildings.forms %}</p>
  
  <pre><code> {{ building }}
  
   {% if building.nested %}   
     {% for formset in building.nested %}   
       {{ formset.as_table }}   
     {% endfor %}   
   {% endif %}
  </code></pre>
    
    <p>{% endfor %}</p>
  </blockquote>
  
  <p>views.py:</p>
  
  <pre><code>def should_delete(self, form):
      """Convenience method for determining if the form’s object will
      be deleted; cribbed from BaseModelFormSet.save_existing_objects."""
  
      if self.can_delete:
          raw_delete_value = form._raw_value(DELETION_FIELD_NAME)
          should_delete = form.fields[DELETION_FIELD_NAME].clean(raw_delete_value)
          return should_delete
  
      return False
  
  def save_all(self, commit=True):
      """Save all formsets and along with their nested formsets."""
  
      # Save without committing (so self.saved_forms is populated)
      # — We need self.saved_forms so we can go back and access
      #    the nested formsets
      objects = self.save(commit=False)
  
      # Save each instance if commit=True
      if commit:
          for o in objects:
              o.save()
  
      # save many to many fields if needed
      if not commit:
          self.save_m2m()
  
      # save the nested formsets
      for form in set(self.initial_forms + self.saved_forms):
          if self.should_delete(form): continue
  
          for nested in form.nested:
              nested.save(commit=commit)
  </code></pre>
  
  <p>forms.py:</p>
  
  <pre><code>def should_delete(self, form):
      """Convenience method for determining if the form’s object will
      be deleted; cribbed from BaseModelFormSet.save_existing_objects."""
  
      if self.can_delete:
          raw_delete_value = form._raw_value(DELETION_FIELD_NAME)
          should_delete = form.fields[DELETION_FIELD_NAME].clean(raw_delete_value)
          return should_delete
  
      return False
  
  def save_all(self, commit=True):
      """Save all formsets and along with their nested formsets."""
  
      # Save without committing (so self.saved_forms is populated)
      # — We need self.saved_forms so we can go back and access
      #    the nested formsets
      objects = self.save(commit=False)
  
      # Save each instance if commit=True
      if commit:
          for o in objects:
              o.save()
  
      # save many to many fields if needed
      if not commit:
          self.save_m2m()
  
      # save the nested formsets
      for form in set(self.initial_forms + self.saved_forms):
          if self.should_delete(form): continue
  
          for nested in form.nested:
              nested.save(commit=commit)
  </code></pre>
  
  <h2>Notes:</h2>
  
  <ul>
  <li><p>I have already looked into the django documentation at <a href="https://docs.djangoproject.com/en/dev/topics/forms/formsets/#understanding-the-managementform" rel="nofollow">https://docs.djangoproject.com/en/dev/topics/forms/formsets/#understanding-the-managementform</a> and did not find anything too useful that discusses how these values are automatically populated by DJANGO</p></li>
  <li><p>I am using Django V1.5</p></li>
  </ul>
  
  <h2>Questions:</h2>
  
  <p>In the event that there is no POST data and the form is being generated solely from the database, how should the 'form-TOTAL_FORMS' &amp;&amp; 'form-INITIAL_FORMS' data be correctly filled in to solve this failure?</p>