Reputation: 131
I have separate python module for signal receivers, it's called signals.py
and imported in ready()
method of my AppConfig
.
In this module I implemented post_save
and post_delete
signal receivers for specific model and registered them via decorator:
@receiver(post_save, sender=MyModel)
def generate_smth(sender, instance, created, **kwargs):
...
And it works fine.
But, when I added to signals.py
receivers of same signals in the same manner, but from different specific models:
@receiver(post_save, sender=AnotherModel)
def generate_smth(sender, instance, created, **kwargs):
...
My functions stopped to receive signals. But if I move receivers into separate python modules mymodel_signals.py
and anothermodel_signals.py
and import both modules in ready()
then all of them works again.
Why it isn't possible to keep receivers in one module?
Upvotes: 0
Views: 1075
Reputation: 46
@receiver(post_save, sender=MyModel)
@receiver(post_save, sender=AnotherModel)
def generate_smth(sender, instance, created, **kwargs):
if sender.__name__ = 'MyModel':
# Bar
else:
# Foo
Upvotes: 1
Reputation: 631
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)
Upvotes: 0