Fixed #27566 -- Clarified overriding ModelAdmin.save_model()/delete_model() docs.

This commit is contained in:
Doug Harris 2016-12-02 13:52:24 -05:00 committed by Tim Graham
parent 07ac5d830a
commit 413216fb9f
1 changed files with 12 additions and 7 deletions

View File

@ -1326,15 +1326,18 @@ templates used by the :class:`ModelAdmin` views:
.. warning:: .. warning::
:meth:`ModelAdmin.save_model` and :meth:`ModelAdmin.delete_model` must When overriding :meth:`ModelAdmin.save_model` and
save/delete the object, they are not for veto purposes, rather they allow :meth:`ModelAdmin.delete_model`, your code must save/delete the
you to perform extra operations. object. They aren't meant for veto purposes, rather they allow you to
perform extra operations.
.. method:: ModelAdmin.save_model(request, obj, form, change) .. method:: ModelAdmin.save_model(request, obj, form, change)
The ``save_model`` method is given the ``HttpRequest``, a model instance, The ``save_model`` method is given the ``HttpRequest``, a model instance,
a ``ModelForm`` instance and a boolean value based on whether it is adding a ``ModelForm`` instance, and a boolean value based on whether it is adding
or changing the object. Here you can do any pre- or post-save operations. or changing the object. Overriding this method allows doing pre- or
post-save operations. Call ``super().save_model()`` to save the object
using :meth:`.Model.save`.
For example to attach ``request.user`` to the object prior to saving:: For example to attach ``request.user`` to the object prior to saving::
@ -1343,12 +1346,14 @@ templates used by the :class:`ModelAdmin` views:
class ArticleAdmin(admin.ModelAdmin): class ArticleAdmin(admin.ModelAdmin):
def save_model(self, request, obj, form, change): def save_model(self, request, obj, form, change):
obj.user = request.user obj.user = request.user
obj.save() super(ArticleAdmin, self).save_model(request, obj, form, change)
.. method:: ModelAdmin.delete_model(request, obj) .. method:: ModelAdmin.delete_model(request, obj)
The ``delete_model`` method is given the ``HttpRequest`` and a model The ``delete_model`` method is given the ``HttpRequest`` and a model
instance. Use this method to do pre- or post-delete operations. instance. Overriding this method allows doing pre- or post-delete
operations. Call ``super().delete_model()`` to delete the object using
:meth:`.Model.delete`.
.. method:: ModelAdmin.save_formset(request, form, formset, change) .. method:: ModelAdmin.save_formset(request, form, formset, change)