Improve ModelAdmin.save_model() [PATCH]
|Reported by:||sebastian_noack||Owned by:||nobody|
|Has patch:||yes||Needs documentation:||yes|
|Needs tests:||no||Patch needs improvement:||yes|
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.
Change History (8)
Changed 4 years ago by sebastian_noack
comment:1 Changed 4 years ago by jacob
- Needs documentation set
- Needs tests unset
- Patch needs improvement set
- Triage Stage changed from Unreviewed to Accepted