Django Passing Custom Form Parameters to Formset

I would use functools.partial and functools.wraps:

from functools import partial, wraps
from django.forms.formsets import formset_factory

ServiceFormSet = formset_factory(wraps(ServiceForm)(partial(ServiceForm, affiliate=request.affiliate)), extra=3)

I think this is the cleanest approach, and doesn’t affect ServiceForm in any way (i.e. by making it difficult to subclass).

Leave a Comment