-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
19 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
|