Opened 14 years ago

Closed 11 years ago

#13708 closed New feature (duplicate)

Improve ModelAdmin.save_model() [PATCH]

Reported by: Sebastian Noack Owned by: nobody
Component: contrib.admin Version: 1.2
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: yes
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description

The ModelAdmin class provides a save_model() method, which can be overriden easily to add some pre- or post-save hooks to the admin. This is a good think and essential for some custom admins. But this API seems to be incomplete.


  1. There is no delete_model() method. If you can change the behaviour of saving a model instance though the admin why not doing the same for deleting an instance?
  1. You can do anything in overriden save_model() methods, even skipping the save operation. Ususually custom form validation, is the way to go, if you want prevent the user from saving the data under some circumstences. But there are use cases, where you need a pre-save hook, which is only called after the form was validated successful, just before the model instance is saved into the database. This hook might fail for some reasons, and you don't want to save the instance in this case. You can implement it this way already, but the code calling save_model() does not care whether the model instance was saved succesful or not, right now. So an admin.LogEntry and an auth.Message object, claiming that the object was added/changed succesdful is created, doesn't matter if it was saved or not. This can be handled by letting save_model() and delete_model() return a boolean indicating, whether the instance was really saved or deleted.

Attachments (1)

admin_improve_save_model.patch (5.5 KB ) - added by Sebastian Noack 14 years ago.

Download all attachments as: .zip

Change History (8)

by Sebastian Noack, 14 years ago

comment:1 by Jacob, 14 years ago

Needs documentation: set
Patch needs improvement: set
Triage Stage: UnreviewedAccepted

comment:2 by Sebastian Noack, 14 years ago

You have set "Patch needs improvement". So please comment, what's wrong with the patch?

comment:3 by anonymous, 14 years ago

Hi, you might wanna take a look at #11108 too. You could steal some of the docs there… Though I am not sure yet whether I like that boolean flag or not…

comment:4 by Julien Phalip, 13 years ago

Severity: Normal
Type: New feature

comment:5 by Aymeric Augustin, 12 years ago

UI/UX: unset

Change UI/UX from NULL to False.

comment:6 by Aymeric Augustin, 12 years ago

Easy pickings: unset

Change Easy pickings from NULL to False.

comment:7 by Tim Graham, 11 years ago

Resolution: duplicate
Status: newclosed

The delete_model hook was added in #11108. I don't think point 2 is worthwhile as it'll be backwards incompatible.

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