Django 信号 m2m_changed 未触发
我最近开始在我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是一个未解决的错误: https://code.djangoproject.com/ticket/16073
我浪费了这周的时间。
This is an open bug: https://code.djangoproject.com/ticket/16073
I wasted hours on it this week.
您将连接它两次,一次使用
m2m_changed.connect
,另一次使用接收器装饰器。You are connecting it twice, once with
m2m_changed.connect
and the other time with receiver decorator.不确定它是否有帮助,但以下内容对我有用:
Not sure if it will help, but the following is working for me:
我不确定这是否有帮助,但您确定在这种特殊情况下应该使用 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.