Provide different kwargs for each form in formset
<p>I am trying to use a formset to create forms for a set of timeframes related to dates:</p>

<pre><code>class Event(models.Model):
   date = models.DateField()

class TimeFrame(models.Model):
   start = models.DateTimeField()
   end = models.DateTimeField()
   event = models.ForeignKey('Event')
</code></pre>

<p>I have code that gets me a queryset of timeframes for each event and added a kwarg to pass this into my form:</p>

<pre><code>class SelectDatesForm(forms.Form):
    timeframes = forms.ModelChoiceField(queryset=HostTimeFrame.objects.none())

    def __init__(self, *args, **kwargs):
       qs = kwargs.pop('timeframes')
       super(SelectDatesForm, self).__init__(*args, **kwargs)
       self.fields['timeframes'].queryset = qs
</code></pre>

<p>Now I'm trying to construct a formset that lets me show timeframes for multiple events on one page. I already found <a href="https://stackoverflow.com/questions/1897756/different-initial-data-for-each-form-in-a-django-formset">this</a> question, explaining how to pass initial data, for serveral forms, but its not the same as passing it to a queryset. </p>

<p>Also there is <a href="https://docs.djangoproject.com/en/1.9/topics/forms/formsets/#passing-custom-parameters-to-formset-forms" rel="nofollow noreferrer">this</a> new function from django 1.9 but it doesnt allow me to get different querysets for each form.</p>

<p>UPDATE:
I got the solution from the answer working, however, whenever im running formset.is_valid() i get the error: </p>

<blockquote>
  <p>Select a valid choice. That choice is not one of the available
  choices.</p>
</blockquote>

<p>Here is what I do in my view:</p>

<pre><code>timeframes = [HostTimeFrame.objects.all()]
SelectDatesFormset = formset_factory(form=SelectDatesForm, extra=len(timeframes), formset=BaseSelectDatesFormSet)
if request.method == 'POST':
    formset = SelectDatesFormset(request.POST, form_kwargs={'timeframes_list': timeframes})
    if formset.is_valid():
        # do something with the formset.cleaned_data
        print(formset)
        pass
else:
    formset = SelectDatesFormset(form_kwargs={'timeframes_list': timeframes})
</code></pre>

<p>Ive been trying for hours to find where this actual validation is done, but i cant find it for the live of me.</p>

<p>Edit: I tested this with the singular form, and i have the same issue, I created a new question for this <a href="https://stackoverflow.com/questions/41005723/select-a-valid-choice-modelchoicefield">here</a>.</p>