Search code examples
djangodjango-signals

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 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?


Solution

  • 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.