12

I recently started to use signals in my Django project (v. 1.3) and they all work fine except that I just can't figure out why the m2m_changed signal never gets triggered on my model. The Section instance is edited by adding/deleting PageChild inline instances on an django admin form.

I tried to register the callback function either way as described in the documentation, but don't get any result.

Excerpt from my models.py

from django.db import models
from django.db.models.signals import m2m_changed


class Section(models.Model):
    name = models.CharField(unique = True, max_length = 100)
    pages = models.ManyToManyField(Page, through = 'PageChild')

class PageChild(models.Model):
    section = models.ForeignKey(Section)
    page = models.ForeignKey(Page, limit_choices_to = Q(is_template = False, is_background = False))


@receiver(m2m_changed, sender = Section.pages.through)
def m2m(sender, **kwargs):
    print "m2m changed!"

m2m_changed.connect(m2m, sender = Section.pages.through, dispatch_uid = 'foo', weak = False)

Am I missing something obvious?

user543424
  • 121
  • 1
  • 3

4 Answers4

12

This is an open bug: https://code.djangoproject.com/ticket/16073

I wasted hours on it this week.

jblaine
  • 520
  • 5
  • 8
3

You are connecting it twice, once with m2m_changed.connect and the other time with receiver decorator.

mipe34
  • 5,596
  • 3
  • 26
  • 38
vijay shanker
  • 2,517
  • 1
  • 24
  • 37
2

Not sure if it will help, but the following is working for me:

class Flow(models.Model):
    datalist = models.ManyToManyField(Data)

from django.db.models.signals import post_save, pre_delete, m2m_changed

def handle_flow(sender, instance, *args, **kwargs):
    logger.debug("Signal catched !")

m2m_changed.connect(handle_flow, sender=Flow.datalist.through)
Stéphane
  • 2,068
  • 22
  • 28
  • This one is different from what the user asked. What the original question was referring to was a scenario where he was using an explicit Model "PageChild" to specify the relation here. – kingpin Mar 26 '14 at 09:50
  • 1
    Even without using the explicit 'through' argument this signal will fail if you use inline admin forms like StackedInline. – stealthwang Dec 02 '14 at 14:57
0

I'm not sure if this will help, but are you sure that you should use Sender.pages.through for this special case? perhaps if you tried @reciever(m2m_changed, sender=PageChild)

Note: if you have @reciever, you do not need m2_changed.connect(...) as @reciever already performs the connect operation.

Thomas
  • 11,757
  • 4
  • 41
  • 57
  • 1
    I tried it with PageChild too, but didn't change anything. Problem is that neither of two ways to register the callback works, that's why I included both. :/ – user543424 Jun 24 '11 at 06:51