I have a question regarding signals in Django
.
According to the documentation
Django
there are two ways to connect a receiver to a signal inDjango
, the first would be with the methodconnect()
- (Signal.connect()
) and the second way, which would alternatively be used, would be a decoratorreceiver()
My question is which is the best option to use? And what are the advantages and disadvantages of using the respective option, either with the method connect()
or with the decorator receiver()
?
In my opinion, the second way would be a better option, with the decorator receiver()
, specifically because of the syntax that must be used to carry it out.
The only difference I find between using method 1 or 2 is the syntax, no more. That's why I have my doubt.
Thanks in advance for your answers!
If you look at the decorator implementation
receiver
( here ):we see something interesting.
If the parameter you pass to the decorator is a single token, it is limited to using
signal.connect()
the decorated function (code after theelse:
) to register as the receiver, so in this case it is fully equivalent to.connect()
.But if you pass it a list or tuple of signals, then it will iterate through it and register the decorated function as a receiver for each signal in that list (code after
if isinstance()...
)