title: Django: Dynamically set Formset for step in Form Wizard
body: <p>As part of a <a href="https://docs.djangoproject.com/en/1.5/ref/contrib/formtools/form-wizard/" rel="noreferrer">Form Wizard</a> in my Django view I am using a <a href="https://docs.djangoproject.com/en/1.5/ref/forms/models/#django.forms.models.modelformset_factory" rel="noreferrer">Formset</a>. The wizard's forms  for each step are declared like this:</p>
  <pre><code>UserFormSet = modelformset_factory(account_models.MyUser,
                                     form=account_forms.MyUserForm, 
                                     extra=5, 
                                     max_num=10, 
                                     can_delete=True)
  
  FORMS = [('userchoice', UserChoiceForm),
           ('user', UserFormSet),]
  TEMPLATES = {'userchoice': &quot;account/userchoice.html&quot;,
               'user': &quot;account/user.html&quot;,}
  </code></pre>
  <p>What I am trying to achieve is this: In UserChoiceForm (first step) the number of required users can be set. I want to use this value to dynamically set the <strong>extra</strong> attribute on UserFormSet so that only the required number of Forms gets displayed in the second step.</p>
  <p>I am trying to do this by overriding the wizard's <a href="https://docs.djangoproject.com/en/1.5/ref/contrib/formtools/form-wizard/#advanced-wizardview-methods" rel="noreferrer">get_form()</a> method:</p>
  <pre><code>class MyUserWizard(SessionWizardView):
   def get_form(self, step=None, data=None, files=None):
       form = super(MyUserWizard, self).get_form(step, data, files)
       
       # Determine the step if not given
       if step is None:
           step = self.steps.current
  
       if step == 'user':
           # Return number of forms for formset requested 
           # in previous step.
           userchoice = self.get_cleaned_data_for_step('userchoice')
           num_users = userchoice['num_users']
           CoFunderFormSet.extra = num_users
           return CoFunderFormSet
       return form
  </code></pre>
  <p>With this approach I am able to get the right amount of forms displayed for the second step, but when trying to post the Formset I am ending up with this error:</p>
  <pre><code>[u'ManagementForm data is missing or has been tampered with']
  </code></pre>
  <p>The POST data has the expected management form fields set, e.g.</p>
  <pre><code>form-TOTAL_FORMS    u'1'
  </code></pre>
  <p>but I assume the FormWizard is using the Formset that was set in the initial FORMS list and therefore the management forms do not match.</p>
  <p>I was wondering if there is a solution to this and if there is a way to tell the FormWizard to use the dynamically generated Formset on POST instead.</p>