I wanted to ensure that a many-to-many field on my model contained a value that was on a foreign key field on the model whenever that model was saved in the admin, like so:
I thought this was a trivial task. I added this code to a
post_save signal connected the model, but the participants list was not being updated. I was pretty sure that the reason it didn’t work was that
form.save_m2m() must be called somewhere in the admin after the object was saved, which would override my m2m changes with the empty ones from the model.. Reading the Django admin source code confirmed this. But it didn’t show an obvious way to circumvent the behavior.
It is possible to override the
add_view functions on the
ModelAdmin object, but I did not want to do that. I would have had to copy the entire
change_view contents into the subclass, as there is no way to make a super call do what I wanted here. Here’s the section of code I needed to modify (it’s in
django.contrib.admin.options.ModelAdmin.change_view if you want to look):
if all_valid(formsets) and form_validated: self.save_model(request, new_object, form, change=True) form.save_m2m() # Right here is where i want to insert my call for formset in formsets: self.save_formset(request, form, formset, change=True) change_message = self.construct_change_message(request, form, formsets) self.log_change(request, new_object, change_message) return self.response_change(request, new_object)
Obviously, I can’t override
save_m2m() is called after that, which would still wipe out my changes. I really need to have a
self.after_m2m() call at the point I have commented in the above code. But I don’t.
I really didn’t want to have to copy this entire method into my
ModelAdmin subclass (in
admin.py) just to add that one call… so instead, I overroad another method that happens to have access to the new object and is called after
save_m2m(). See that call to
self.log_change a few lines later? That method updates the admin log db table. But it also happens to have access to the newly created object. I want to emphasize that this is an ugly hack:
# in admin.py class ClassListAdmin(admin.ModelAdmin): # def log_change(self, request, object, message): super(ClassListAdmin, self).log_change(request, object, message) # The add and change views happen to call log_addition and # log_change after the object has been saved. I update the # m2m at this point (in update_participants) because the add # and change views call form.save_m2m() which wipes out the # changes if I put it in self.save_model(). self.update_participants(object)
self.update_participants(), of course, contains the code I originally wanted to run.
This isn’t the most proper way to do this, but if you’re looking for a quick, dirty but DRY hack, it might save you some time.