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?

4

4 回答 4

12

这是一个开放的错误:https ://code.djangoproject.com/ticket/16073

这周我在这上面浪费了几个小时。

于 2012-01-19T18:40:52.900 回答
3

您将它连接两次,一次与接收器装饰器连接,m2m_changed.connect另一次与接收器装饰器连接。

于 2013-02-08T09:07:28.923 回答
2

不确定它是否会有所帮助,但以下内容对我有用:

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)
于 2011-09-01T14:27:38.737 回答
0

我不确定这是否会有所帮助,但是您确定应该在这种特殊情况下使用 Sender.pages.through 吗?也许如果你尝试过@reciever(m2m_changed, sender=PageChild)

注意:如果你有@reciever,你不需要 m2_changed.connect(...) 因为@reciever 已经执行了连接操作。

于 2011-06-24T06:24:24.373 回答