title: Django: How to filter ForeignKey choices (e.g. with request.user) with ModelFormSet and FormWizard?
body: <p>Having several ForeignKeys in a <code>ModelForm</code> that I want to use in a form wizard with <code>modelformset_factory</code> (not 100% sure about the formset) I'm wondering how to limit the choices of the dropdown field for I need to do it dynamically. I wanted to try it by writing my own modelformset factory but here on stackoverflow I read about other approaches but unfortunatelly I don't understand them.</p>
  
  <p>That's how far I came:</p>
  
  <p>models.py</p>
  
  <pre><code>#...
  class Attendee(models.Model):
      """Event specific attendee details."""
      # event is set by URL.
      event = models.ForeignKey(Event)
      attendee = models.ForeignKey(Person) # Contact details, should be limited to user
      accommodation = models.ForeignKey(Accommodation, blank=True) # *
      workshop = models.ForeignKey(Workshop, blank=True) # *
      volunteer = models.ForeignKey(Volunteer, blank=True) # *
      # *= should be limited to event but I think I will be able to handle that.
      #...
  
  class AttendeeForm(forms.ModelForm):
      class Meta:
          model = Attendee
  
      def __init__(self, *args, **kwargs):
  #    def __init__(self, user, *args, **kwargs): I tried this but I didn't get 
  # it to work.
          super(AttendeeForm, self).__init__(*args, **kwargs)
  #         self.fields['attendee'].queryset = Person.objects.filter(owner=user)
          self.fields['workshop'].required = True
          #...
  
  # This is from https://stackoverflow.com/a/4858044/2704544 but I don't understand it. ##
  def form_setup(**kwargs):
      def make_form(data, prefix=None, initial=None):
          form = (data, prefix, initial)
          for k, v in kwargs.items():
              if k == 'some_list':
                  form.fields['some_list'].choices = v # What does that mean?
              ...
          return form
      return make_form
  #######################################################################################
  </code></pre>
  
  <p>(<a href="https://stackoverflow.com/a/4858044/2704544">link</a> to source)</p>
  
  <p>views.py</p>
  
  <pre><code># This is from https://stackoverflow.com/a/623198/2704544 but I don't understand it. ###
  class Callback(object):
      def __init__(self, field_name, aff):
          self._field_name = field_name
          self._aff = aff # What is this aff?
      def cb(self, field, **kwargs):
          nf = field.formfield(**kwargs)
          if field.name == self._field_name:  # this is 'options' field
              nf.queryset = ServiceOption.objects.filter(affiliate=self._aff)
          return nf
  #######################################################################################
  
  reg_wiz_forms = (
      ('attendees', modelformset_factory(Attendee, form=AttendeeForm, exclude='event', 
  # formfield_callback=Callback('option', affiliate).cb Just copied. Why can you call 
  # cb without arguments?
  )),)
  class RegWizard(SessionWizardView):
      def get_form_instance(self, step):
          instance = None
  
          if step == '0':
              instance = Attendees.objects.filter(owner__owner=self.request.user)
          #...
  </code></pre>
  
  <p>(<a href="https://stackoverflow.com/a/623198/2704544">link</a> to source)</p>
  
  <p>urls.py</p>
  
  <pre><code>#...
  url(r'^register/$', views.RegWizard.as_view(views.reg_wiz_forms), name='register'),
  #...
  </code></pre>
  
  <p>I also <a href="https://stackoverflow.com/a/624013/2704544">read</a> about <code>curry</code> and other stuff and tried to override <code>get_initkwargs</code> and other methods of <code>WizardView</code> but I couldn't find any more docu or hints on that topic. Maybe someone can help me out.</p>
  
  <h2>Update</h2>
  
  <p>It works partly with <code>curry</code> now. Partly because it doesn't work with a manage function:</p>
  
  <p>views.py</p>
  
  <pre><code>def manage_wizard(request, event):
      AttendeeFormSet = modelformset_factory(Attendee, form=AttendeeForm, exclude='event')
      AttendeeFormSet.form = staticmethod(curry(AttendeeForm, user=request.user))
      wiz = RegWizard
      wiz.event = event
      return wiz.as_view([('attendees', AttendeeFormSet)])
  </code></pre>
  
  <p>I get an AttributeError: "'function' object has no attribute 'base_fields'" This sounds like the same issue <a href="https://stackoverflow.com/a/624013/2704544">here</a>.</p>
  
  <p>But when I override <code>WizardView</code>'s <code>get_form_list</code> and call it in url.py directly it works:</p>
  
  <pre><code>reg_wiz_forms = ('attendees', modelformset_factory(Attendee, form=AttendeeForm, exclude='event'))
  
  class RegWizard(SessionWizardView):
      def get_form_list(self):
          self.form_list['attendees'].form = staticmethod(curry(AttendeeForm, user=self.request.user))
          return super(RegWizard, self).get_form_list()
  </code></pre>
  
  <p>Now I'm wondering if there is a solution without overriding the method.</p>