Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Consolidating multiple post_save signals with one receiver

Tags:

django

So I read the Django source code (post 1.5) that you can now register multiple multiple signals to a receiver function:

def receiver(signal, **kwargs):     """     A decorator for connecting receivers to signals. Used by passing in the     signal (or list of signals) and keyword arguments to connect::          @receiver(post_save, sender=MyModel)         def signal_receiver(sender, **kwargs):             ...          @receiver([post_save, post_delete], sender=MyModel)         def signals_receiver(sender, **kwargs):             ...      """     ... implementation code... 

However, I want to register multiple post_save signals from different senders to the same function. Right now, I just call

post_save.connect(fn_name, model_name) 

for each model that I have. Is there a better way to do this with the new Django 1.5 @receiver decorator capability?

like image 677
fangsterr Avatar asked Jul 06 '13 22:07

fangsterr


1 Answers

You can do that using the @receiver decorator:

from django.dispatch import receiver  @receiver(post_save, sender=Model1) @receiver(post_save, sender=Model2) @receiver(post_save, sender=Model3) def my_signal_handle(sender , **kwargs)     # some code here 
like image 109
hubert Avatar answered Oct 07 '22 02:10

hubert