Skip to content

Commit

Permalink
Overriding save behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
shabda committed Feb 20, 2018
1 parent bbd4a83 commit f53c3eb
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion docs/override_save.rst
Original file line number Diff line number Diff line change
@@ -1,2 +1,20 @@
How to override save behaviour for Django admin?
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++++++++++++++++++++++

:code:`ModelAdmin` has a :code:`save_model` method, which is used for creating and updating model objects.
By overriding this, you can customize the save behaviour for admin.


The :code:`Hero` model has the following field.::

added_by = models.ForeignKey(settings.AUTH_USER_MODEL,
null=True, blank=True, on_delete=models.SET_NULL)


If you want to always save the current user whenever the Hero is updated, you can do.::

def save_model(self, request, obj, form, change):
obj.added_by = request.user
super().save_model(request, obj, form, change)


0 comments on commit f53c3eb

Please sign in to comment.