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?
+2
A:
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.
Yuval A
2010-09-05 16:27:17
+2
A:
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
lazerscience
2010-09-05 17:12:41