(Model)formsets, passing custom kwargs to form and _construct_form

196 views
Skip to first unread message

Sergei Maertens

unread,
Mar 31, 2015, 12:42:40 PM3/31/15
to django-d...@googlegroups.com
Hi all,

I've quite often found myself needing to pass custom kwargs to each form within a formset (common parameter for all forms). A common pattern for this is implementing a custom BaseMyModelFormSet(BaseModelFormSet), overriding __init__ and then passing the custom argument via _construct_form, hereby overriding/extending a private method.

Would it be a good idea to add a form_kwargs attribute on the BaseFormSet class, and pass those arguments by default within _construct_form? This would reduce code verbosity so that you only need to set form_kwargs, either on class definition of the custom BaseFormSet, or dynamically in the custom formset class' __init__. This would prevent overriding a private method.

An alternative approach is using functools' partial/curry, but apparently this may cause trouble with the order of arguments and passing dynamic values (such as request.user).

I'd love to hear any feedback on this, if other people like it I might give the implementation a go on the Djangocon EU sprint.

Sergei Maertens

unread,
Jun 4, 2015, 6:09:22 AM6/4/15
to django-d...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages