Django 1.8 - Signals - What's the difference between @receiver decoder and Signal.connect () method?

They seem to be doing the same. Is there a difference in functionality, usage, etc.? Under what circumstances should each other be used?



source to share

2 answers


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



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



Alternatively, you can use the receiver decorator ():

from django.core.signals import request_finished
from django.dispatch import receiver

def my_callback(sender, **kwargs):
    print("Request finished!")




All Articles