开发者

Custom signal does not work

I created a signal:

sig_published = Signal()

This signal is placed in a signals.py, which I import in my models.py:

from signals import sig_published

and in the model file's开发者_Go百科 footer, I connect it to a function which for testing purposes just prints out a debug string.:

def update_mode(sender, **kwargs):
    print "UPDATING"
sig_published.connect(update_mode, sender=MyModel)

Now, in my save()-Method, I try to send a signal:

sig_published.send(sender=self)

but it does not print out anything. If I try a built-in signal like pre_save:

pre_save.connect(update_mode, sender=MyModel)

it works. Any ideas?


You're emitting the signal with sender equal to your model's instance, while in connect you're matching against MyModel class. Those two aren't the same object, so your receiver ignores the signal. You can compare with pre_save emitting code, that it uses a class, not an instance.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜