'how to tell if a screen is add or change in django admin
I am trying to determine if the admin screen in Django is add or change in the save method. If I internet search on this, I cannot find any answer. What is the proper way to do this in Python?
Solution 1:[1]
As with ModelForms, you can use a similar trick:
def save_model(self, request, obj, form, change):
if obj._state.adding:
# Adding
else:
# Editing
super().save_model(request, obj, form, change)
Solution 2:[2]
We can check self.instance but just before super().init() is called. Try out my solution https://stackoverflow.com/a/70845558/15080117
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | j4n7 |
| Solution 2 | Ivan Vinitskyi |
