11赞
289
当前位置:  开发笔记 > 编程语言 > 正文

djangom2m_changed信号与定制通过模型-djangom2m_changedsignalwithcustomthroughmodel

Iamtryingtousethem2m_changedsignaltotriggersomeactionsinmyapplication.However,thep

I am trying to use the m2m_changed signal to trigger some actions in my application . However, the printout of signaltest() indicates that I am only signalled on pre_clear and post_clear actions. My models.py looks like this:

我试图使用m2m_changed信号来触发我的应用程序中的一些操作。但是,signaltest()的打印输出表明我只是在pre_clear和post_clear动作上发出信号。我的models.py看起来像这样:

class Entry(models.Model):
    objects = managers.MyEntryManager()
    ...
    fields = models.ManyToManyField('Field', through='EntryField')

class Field(models.Model):
    name = models.CharField(max_length=64, unique=True)
    description = models.CharField(max_length=256, blank=True)

class EntryField(models.Model):
    entry = models.ForeignKey('Entry')
    field = models.ForeignKey('Field')
    value = models.CharField(max_length=256)

def signaltest(**kwargs):
    print kwargs['action']
signals.m2m_changed.connect(signaltest, sender=Entry.fields.through, weak=False, dispatch_uid='signaltest')

The EntryField objects are created elsewhere in the code using the following code:

使用以下代码在代码中的其他位置创建EntryField对象:

some_entry.fields.clear()
models.EntryField.objects.get_or_create(
    entry=some_entry,
    field=some_field,
    defaults = { 'value': field_value }
)

The first call is responsible for the pre_clear and post_clear events I receive. However, the second call generates no signals.

第一个电话负责我收到的pre_clear和post_clear事件。但是,第二次调用不会产生任何信号。

It seems to me that django bug #13757 is related to this (mis)behaviour, but I may be missing something.

在我看来,django bug#13757与这种(错误)行为有关,但我可能会遗漏一些东西。

Is there a way to "rewire" the signals (perhaps using some signal other than m2m_changed) to have post_save signals generated when a EntryField is created?

有没有办法“重新连接”信号(可能使用除m2m_changed之外的一些信号)来创建EntryField时生成的post_save信号?

1 个解决方案

#1


0  

could you please try this out?

你能试试吗?

def signaltest(**kwargs):
   print kwargs['instance']
   print kwargs['created']

signals.post_save.connect(signaltest, sender=EntryField, weak=False, dispatch_uid='signaltest')

推荐阅读
author-avatar
竹叶清2012
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有