Re: [Django] #10874: ModelFormMetaclass does not provide easy way of extending

5 views
Skip to first unread message

Django

unread,
Mar 5, 2022, 6:22:15 AM3/5/22
to django-...@googlegroups.com
#10874: ModelFormMetaclass does not provide easy way of extending
-----------------------------+------------------------------------
Reporter: wombat | Owner: nobody
Type: New feature | Status: new
Component: Forms | Version: 1.0
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------+------------------------------------

Comment (by Matt Westcott):

For anyone looking for a way to extend ModelFormOptions with their own
options, I found this recipe works well: https://github.com/wagtail
/django-permissionedforms/blob/main/permissionedforms/forms.py

There's a slight bit of ugliness in the implementation, in that
ModelFormMetaclass builds a ModelFormOptions instance to do its own thing,
which then gets thrown away and replaced with the custom ModelFormOptions
subclass, meaning that the ModelFormOptions constructor ends up running
for a second time. This could be avoided if ModelFormMetaclass were to
look for an `options_class` attribute on either the target class (as per
above) or itself (as per my implementation).

(Even better, Django could move the 'collect options from an inner Meta
class' logic into a mixin as I've done, so that the pattern of having
various form mixin classes contribute their own options can happen with or
without ModelForm. But given the lack of activity on this issue, that's
probably too niche an interest to be worth the disruption :-) )

--
Ticket URL: <https://code.djangoproject.com/ticket/10874#comment:6>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Reply all
Reply to author
Forward
0 new messages