[Answer]-Several Django signal receivers in one module

1👍

@receiver(post_save, sender=MyModel)
@receiver(post_save, sender=AnotherModel)
def generate_smth(sender, instance, created, **kwargs):
    if sender.__name__ = 'MyModel':
       # Bar
    else:
      # Foo

0👍

Do you want both functions to have the same behaviour? If yes, you can do:

def do_smth(sender, instance, created, **kwargs):
    ...

@receiver(post_save, sender=MyModel)
def generate_smth(sender, instance, created, **kwargs):
    do_smth(sender, instance, created, **kwargs)

@receiver(post_save, sender=AnotherModel)
def generate_another_smth(sender, instance, created, **kwargs):
    do_smth(sender, instance, created, **kwargs)

Leave a comment