Opened 13 years ago

Last modified 12 years ago

#8005 closed

Make it easier to alter a model saved by the admin (e.g. add the user that created or updated it) — at Version 1

Reported by: simon Owned by: nobody
Component: contrib.admin Version: master
Severity: Keywords:
Cc: Triage Stage: Design decision needed
Has patch: yes Needs documentation: yes
Needs tests: yes Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description (last modified by simon)

A very common requirement for customising the admin is to automatically update a created_by or updated_by field with the user object that made changes to an object. This is currently way harder than it should be:

I propose adding an extra subclassable method that makes it trivial to intercept the model as it is being created and perform additional work on it. Here's an example (incomplete) patch illustrating the idea:

This would allow ModelAdmin subclasses to make additional changes to the model by calling instead. For the admin user described above, the subclass would look like this:

class ArticleAdmin(admin.ModelAdmin):
    def save_model_add(self, request, form, formsets):
        new_object =
        new_object.created_by = request.user
        new_object.updated_by = request.user
        if formsets:
            for formset in formsets:
                formset.instance = new_object
        return new_object
    def save_model_change(self, request, form, formsets):
        new_object =
        new_object.updated_by = request.user
        if formsets:
            for formset in formsets:
        return new_object

This is a big improvement, although I'm not too happy with the need to duplicate the formset logic. Maybe that should be split out in to a separate over-ridable method as well.

Change History (3)

Changed 13 years ago by simon

Attachment: 8005.1.diff added

comment:1 Changed 13 years ago by simon

Description: modified (diff)

Changed 13 years ago by simon

Attachment: 8005.2.diff added

Improved patch; refactored formset boilerplate in to a separate method

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