Opened 15 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.
- 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?
- 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)
Change History (8)
by , 15 years ago
Attachment: | admin_improve_save_model.patch added |
---|
comment:1 by , 15 years ago
Needs documentation: | set |
---|---|
Patch needs improvement: | set |
Triage Stage: | Unreviewed → Accepted |
comment:2 by , 15 years ago
comment:3 by , 15 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 , 14 years ago
Severity: | → Normal |
---|---|
Type: | → New feature |
comment:7 by , 11 years ago
Resolution: | → duplicate |
---|---|
Status: | new → closed |
The delete_model
hook was added in #11108. I don't think point 2 is worthwhile as it'll be backwards incompatible.
You have set "Patch needs improvement". So please comment, what's wrong with the patch?