Opened 18 years ago

Closed 16 years ago

#4507 closed (fixed)

Adding form save hooks to newforms admin

Reported by: Stas Shtin <antisvin@…> Owned by: nobody
Component: contrib.admin Version: newforms-admin
Severity: Keywords: nfa-someday
Cc: Triage Stage: Design decision needed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Sometimes it's necessary to set DB fields bypassing user input with values based on request object or state of the world. Specific use cases would be saving object creator, last editor, expiration date (i.e. current date + some period of time).
I haven't found any other way for setting DB fields that are not editable with admin application.

Also please notice that ticket #4312 has the same motives as this one, but targeting form_for_model and form_for_instance. Accepting that one would make overloading form save hooks even easier.

Attachments (2)

newforms-admin-form-save-hooks.diff (1.9 KB ) - added by Stas Shtin <antisvin@…> 18 years ago.
model-admin-save-hooks.diff (2.6 KB ) - added by Petr Marhoun <petr.marhoun@…> 17 years ago.

Download all attachments as: .zip

Change History (9)

by Stas Shtin <antisvin@…>, 18 years ago

comment:1 by Chris Beaven, 17 years ago

Triage Stage: UnreviewedDesign decision needed

See #4727 for another solution to this.

comment:2 by Petr Marhoun <petr.marhoun@…>, 17 years ago

I don't think that #4727 solves this problem. If I have model MyModel and base form MyBaseForm, then the method resolution order is:

(<class 'django.newforms.widgets.MyModelForm'>, <class 'my_project.my_app.MyBaseFrom'>, <class 'django.newforms.forms.BaseForm'>, <class 'django.utils.encoding.StrAndUnicode'>, <type 'object'>)

Method django.newforms.widgets.MyModelForm.save is in reality function django.newforms.models.save_instance - save method in MyBaseForm is not called.

by Petr Marhoun <petr.marhoun@…>, 17 years ago

Attachment: model-admin-save-hooks.diff added

comment:3 by Petr Marhoun <petr.marhoun@…>, 17 years ago

The attached patch is compatible with the current branch and has more arguments.

comment:4 by Petr Marhoun <petr.marhoun@…>, 17 years ago

I have two real examples where I need this hook and I don't think that it can be solved with base forms. (All names are changed.)

  1. ParentModel has three child models - ChildModelOne, ChildModelTwo and ChildModelThree. Instances of ChildModelOne and ChildModelTwo are edited by users as inlines. Instances of ChildModelThree are generated from saved instances of ParentModel, ChildModelOne and ChildModelTwo. My code is:
class MyModelOptions(admin.ModelAdmin):
    # ...

    def form_save(self, request, model, form, formsets, commit, add):
        my_object = super(MyModelOptions, self).form_save(request, model, form,
            formsets, commit, add)
        my_object.generate_instances_of_child_model_three()
        return my_object

  1. Second example is the same as in the original description. So I only show my code:
class MyModelOptions(admin.ModelAdmin):
    # ...

    base_form = MyModelBaseForm

    def form_save(self, request, model, form, formsets, commit, add):
        my_object = super(MyModelOptions, self).form_save(request, model, form,
            formsets, False, add)
        my_object.set_other_data(form.other_data)
        my_object.save()
        form.save_m2m()
        return my_object

I use save hook and base form - I need both.

comment:5 by Karen Tracey <kmtracey@…>, 17 years ago

Keywords: nfa-someday added

Near as I can tell this is request for new function that wasn't available in old admin either? As such it should not block newforms-admin merge. If I'm wrong feel free to correct me.

comment:6 by Brian Rosner, 17 years ago

The real solution to this would be with ModelForm. Once newforms-admin uses it instead of the form_for_* functions then it should be made override-able and then you can change the save behavior.

comment:7 by jkocherhans, 16 years ago

Resolution: fixed
Status: newclosed

This has been fixed at some point. See save_form, save_model, and save_formset on the ModelAdmin class.

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