Can we send a request object or keyword argument to modelformset_factory in django 1.9
<p>I have the following view</p>

<pre><code>def application(request, uuid):
    application = get_object_or_404(LoanApplication, uuid=uuid)

    partners = FirmPartner.objects.filter(application=application)
    PartnerFormset = modelformset_factory(FirmPartner, form=FirmPartnerForm, can_delete=True, extra=1)

    if request.method == 'POST':
        .........
        ..........
</code></pre>

<p>But i want to make some fields in FirmPartnerForm required dynamically inside <strong>init</strong> method of form by using some values from request or keyword argument that we passed in like <code>modelformset_factory(......application=application)</code> ?</p>

<p>So is it possible to send request object or any keyword argument to modelformset_factory ? as below</p>

<pre><code>modelformset_factory(FirmPartner, form=FirmPartnerForm, can_delete=True, extra=1, request=request, application=application) ?
</code></pre>

<p><strong><em>Edit</em></strong></p>

<p>Now i upgraded to django 1.9.9 and below is my code</p>

<pre><code>from .forms import FirmPartnerForm

PartnerFormset = modelformset_factory(FirmPartner, form=FirmPartnerForm, can_delete=True, extra=1,
                       form_kwargs={'request': request, 'application': application})
</code></pre>

<p><strong><em>forms.py</em></strong></p>

<pre><code>class FirmPartnerForm(forms.ModelForm):
    class Meta:
        model = FirmPartner
        exclude = ['application']

    def __init__(self, *args, **kwargs):
        super(FirmPartnerForm, self).__init__(*args, **kwargs)
        if kwargs.get('application', None):
            application = kwargs.get('application

            .....................
            .....................
</code></pre>

<p><strong><em>Error Traceback</em></strong></p>

<pre><code>  File "/Users/name/projects/project_name/applications/views.py", line 462, in application_management
    form_kwargs={'request': request, 'application': application})
TypeError: modelformset_factory() got an unexpected keyword argument 'form_kwargs'
</code></pre>

<p>So after <code>Daniel</code> said form_kwargs has been added in 1.9, i have upgraded to 1.9.9 and still i was getting the above error ?</p>

<p>The <a href="https://docs.djangoproject.com/en/1.10/topics/forms/formsets/#passing-custom-parameters-to-formset-forms" rel="nofollow"><code>form_kwargs</code></a> parameter is what you need.</p>

<pre><code>modelformset_factory(FirmPartner, form=FirmPartnerForm, can_delete=True, extra=1,
                     form_kwargs={'request': request, 'application': application})
</code></pre>

python django django-forms modelform