Saturday, February 12, 2011

Django: How to hook in after multiple M2M have been processed

This situation comes up, from time to time, when we need to get something to happen after a many-to-many field is changed. The novice will connect a post_save signal and scratch his head when it doesn't fire on the addition or removal of items in the ManyToManyField. We all learn that it takes a slightly more complicated signal, the m2m_changed signal, and its many actions, which tell us exactly what has changed in the particular field sending it (the signal comes from the field's through table, to be exact).

Well, a slightly more complicated case arose in a design today and I was scratching my head and feeling like a novice all over again. You see, I needed to know when new things had been adding to such a field, but I had more than one. In fact, I had four of them. I needed a specific function called on the instance when all of these fields were finished being cleared or added to or subtracted from. This was in a form in the Django admin.

Thankfully I had an assumption I could make this easier with: the fields in question would only be set from the admin interface. With this knowledge in hand, I hopped into the app's admin.py and I added these methods:

These get called when the admin interface is used to edit and create a new object, respectively. It happens after both the model instance itself and all of the many-to-many fields have been saved, which is exactly what we need. It works, but I wish I could find a better solution.