Opened 11 years ago

Last modified 3 months ago

#10305 new New feature

Add a form instantiation hook for contrib.admin add/change views

Reported by: tangc Owned by: nobody
Component: contrib.admin Version: master
Severity: Normal Keywords: add_view, change_view, ModelForm
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Julien Phalip)

add_view and change_view have the following code:

ModelForm = self.get_form(request)
...
form = ModelForm(request.POST, request.FILES)

or:

 form = ModelForm(initial=initial)

or:

 form = ModelForm(request.POST, request.FILES, instance=obj)

I would propose:

if isinstance(ModelForm, ModelFormMetaclass):
    form = ModelForm(request.POST, request.FILES)
else
    form = ModelForm

This way gives the flexibility to override the function get_form(self, request, obj=None, **kwargs), which could return a modelform_factory(self.model, **defaults) or an instance of a model form.

Attachments (1)

T10305.diff (4.5 KB) - added by tangc 11 years ago.

Download all attachments as: .zip

Change History (10)

Changed 11 years ago by tangc

Attachment: T10305.diff added

comment:1 Changed 11 years ago by Alex Gaynor

Triage Stage: UnreviewedDesign decision needed

My inclination is that this is the wrong way to go about doing this, an "instiate_form" hook or somethign similar is a much better API design IMO.

comment:2 Changed 11 years ago by (none)

milestone: post-1.0

Milestone post-1.0 deleted

comment:3 Changed 10 years ago by Tay Ray Chuan

Summary: contrib/admin/options.py add_view, change_viewchange the way ModelForms are instantiated in add_view, change_view

Changed the summary to be more informative.

comment:4 Changed 9 years ago by Chris Beaven

Severity: Normal
Type: New feature

comment:5 Changed 9 years ago by Julien Phalip

Description: modified (diff)
Easy pickings: unset

Reformatted description to be more readable.

comment:6 Changed 8 years ago by Aymeric Augustin

UI/UX: unset

Change UI/UX from NULL to False.

comment:7 Changed 7 years ago by Jacob

Triage Stage: Design decision neededAccepted

I agree with Alex: the proposed API is clumsy. But the need is real, so moving this to accepted. Determine a better API first though.

comment:8 Changed 5 years ago by Tim Graham

Summary: change the way ModelForms are instantiated in add_view, change_viewAdd a form instantiation hook for contrib.admin add/change views

#17577 is a duplicate.

comment:9 Changed 3 months ago by Romaric Pascal

I've ran into the exact same need and thought it'd be great if ModelAdmin had that instanciate_form hook.

How does the following sound: The calls to ModelForm(...) inside _changeform_view could be replaced by self.get_form_instance(ModelForm, request, obj,...) which would pass any arguments to the ModelForm, but allow developers to intercept the call and maybe set initial values of their own based on the request or the current object.

Actually the get_form_instance default implementation could also be responsible for calling the get_form so that the signature doesn't require the form class to be passed (but that might be too many responsibilities for it and make things confusing).

Would be happy to contribute the change if that was fitting :D

Note: See TracTickets for help on using tickets.
Back to Top