Signals are a very useful part of Django, But I am having a hard time figuring out when they are really necessary. Take the following ambiguous example:
class FooBar(models.Model):
some_field = models.something(#...)
def func(self):
# do something
signals.some_signal.send(#...)
so this will send a signal whenever the func
method is called. But one could also do the following:
def func(self):
# do something
# do what was in the receiver function
Are there any classifications of when to use a signal, and when to just include it in the original location?
It seems as though most situations will work both ways. Are signals just more optimal?
The idea behind signals or events is that some source generates them and someone else consumes them without knowing about each other.
So while your example does work technically, it creates a hard dependency between func()
and the receiver function. If the receiver goes away or changes, you always need to check func()
as well.
With signals, func()
just publishes a signal. Whoever might be interested (and there might be dozens of receivers or none) gets a notification without any change to func()
.