I have a Model with a "status" field. When the user users the Admin app to modify an instance, how to I hook onto the "Save" button click so that I could update "status" to a value that's dependent on the logged in user's username?
Override your modeladmin's save_model
-method:
class ModelAdmin(admin.ModelAdmin):
def save_model(self, request, obj, form, change):
user = request.user
instance = form.save(commit=False)
if not change: # new object
instance.status = ....
else: # updated old object
instance.status = ...
instance.save()
form.save_m2m()
return instance
Use the pre_save
signal. Granted, it will be called on every instance save operation, not only from admin, but it does apply to your situation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With