Thomas Kremmel
Thomas Kremmel

Reputation: 14783

Django Passing Custom Form Parameters to modelformset_factory

based upon this answer regarding formset_factory, I tried to do the same thing for modelformset_factory:

from django.utils.functional import curry
from functools wraps

AccountMemberFormSetBase = modelformset_factory(AccountMember,
                                                form=wraps(AccountMemberLimitedModelForm)(curry(AccountMemberLimitedModelForm, affiliate="test")),
                                                extra=2)

This throws the following error:

function() argument 1 must be code, not str
Exception Location: ../django/forms/models.py in modelform_factory, line 528

Any idea whats wrong here?

Upvotes: 2

Views: 2398

Answers (2)

Rustem Kerimov
Rustem Kerimov

Reputation: 66

class AccountMemberLimitedModelForm(forms.Form):
...     def __init__(self, *args, **kwargs):
...         affiliate = kwargs.pop('affiliate')
...         super(AccountMemberLimitedModelForm, self).__init__(*args, **kwargs)



AccountMemberFormSetBase = formset_factory(AccountMemberLimitedModelForm)
    formset = AccountMemberFormSetBase(form_kwargs={'affiliate': "test"})

Upvotes: 0

RobM
RobM

Reputation: 1035

I found myself in the same situation today.

See my comment in Django Passing Custom Form Parameters to Formset for details and a workaround (I hope a link to StackOverflow itself is acceptable).

Upvotes: 1

Related Questions