On Feb 14, 9:02 pm, Jeff Blaine <cjbla...@gmail.com> wrote: > Hi all, > > I'm having trouble detecting changes that happen to a M2M field behind the > scenes: as a result of one of the *referenced objects being deleted* > > I'm using Django 1.3.1 with PostgreSQL 8 > > Let's say we have the following simple proof of concept models: > > class Topping(models.Model): > name = models.CharField() > > class Pizza(models.Model): > name = models.CharField() > toppings = models.ManyToManyField(Topping, null=true, blank=true) > > And this data established using those models: > > TOPPING > id=1, name="Pepperoni" > id=2, name="Onion" > id=3, name="Mushroom" > > PIZZA > id=1, name="foopizza" > toppings=1,2,3 > > Known Facts: > > 1. Deleting any Topping object (for example, deleting id=1, > name="Pepperoni") also removes it from foopizza.toppings. Good. > 2. No m2m_changed signal is sent when 1 (above happens). BAD. > > How do I tie into "topping *object* was deleted (no longer for sale), > perform custom code on ALL Pizza objects that referenced it" ? > > Register a post_delete signal for Topping? How do I refer to or find the > Pizza objects that referred to it?
It seems like an oversight that no m2m_changed signal is generated in this case. I think this is worth a ticket in trac (https:// code.djangoproject.com/newticket). The problem is that you can't collect the related Pizzas from the post_delete signal. The m2m relations are already deleted at that point. If you can just do the processing in the pre_delete signal then things are much easier, as you can just use instance.pizza_set.all() to get the related objects. - Anssi -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.