They appear to do the same thing. Is there a difference in functionality, usage, etc? In what circumstances should one be used over the other?
Thanks
They indeed do exactly the same thing from a functional point of view. There is no reason to prefer the one over the other whatsoever, apart from how the developer wants to organize the code.
EDIT: As per the excellent answer from @knbk, you should use the connect
function for specific actions, as for example to pass a list of callback functions.
From the Django documentation on signals:
There are two ways you can connect a receiver to a signal. You can take the manual connect route:
from django.core.signals import request_finished request_finished.connect(my_callback)
Alternatively, you can use a receiver() decorator:
from django.core.signals import request_finished from django.dispatch import receiver @receiver(request_finished) def my_callback(sender, **kwargs): print("Request finished!")
@receiver
is a thin wrapper around Signal.connect()
. The only difference is that @receiver
can accept not just a single signal, but also a list
or tuple
of signals, and it will connect the function to each of those signals.
If you take a look at the source code, @receiver
only calls signal.connect(func)
and returns the original function.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With