Django 信号 m2m_changed 未触发

发布于 2024-11-17 05:28:38 字数 855 浏览 7 评论 0原文

我最近开始在我的 Django 项目(v.1.3)中使用信号,它们都工作正常,除了 我只是不明白为什么 m2m_changed 信号永远不会在我的模型上触发。通过在 django 管理表单上添加/删除 PageChild 内联实例来编辑部分实例。

我尝试按照文档中所述的任一方式注册回调函数,但没有得到任何结果。

摘自我的 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)

我是否遗漏了一些明显的东西?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

玻璃人 2024-11-24 05:28:38

这是一个未解决的错误: https://code.djangoproject.com/ticket/16073

我浪费了这周的时间。

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

I wasted hours on it this week.

早茶月光 2024-11-24 05:28:38

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

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

柒夜笙歌凉 2024-11-24 05:28:38

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

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)

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)
鱼忆七猫命九 2024-11-24 05:28:38

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文