Django 1.8 - Signals - What's the difference between @receiver decoder and Signal.connect () method?
@receiver
- thin wrap around Signal.connect()
. The only difference is that it @receiver
can receive not only one signal, but also signals list
or tuple
, and it will connect a function to each of these signals.
If you look at the source code , @receiver
it only calls signal.connect(func)
and returns the original function.
source to share
They actually do the same from a functional point of view. There is no reason to prefer one over the other other than how the developer wants to organize the code.
EDIT: As per the excellent answer from @knbk, you should use a function connect
for certain actions, like passing a list of callback functions.
From the Django documentation on signals :
There are two ways to connect a receiver to a signal. You can use the manual connection route:
from django.core.signals import request_finished request_finished.connect(my_callback)
Alternatively, you can use the 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!")
source to share