I set a receiver on a post_save
signal and I was hoping catching the signals for all the proxy of my Model by setting the sender to the main Model but it does not seem to work:
class MyObject(models.Model):
....
class MyObjectProxy(MyObject):
class Meta:
proxy = True
# The receiver
# How to avoid writing another one for sender=MyObjectProxy ?
@receiver(post_save, sender=MyObject)
...
My receiver is not triggered when that happens:
obj = MyObjectProxy()
obj.save()
Is that normal?
I have to set a receiver for each proxy?
Can I set sender
to a list of models?
Thanks.
Proxy models allow us to change the Python behavior of a model without changing the database. vehicles/models.py. from django.db import models. class Car(models.Model): vin = models.CharField(max_length=17)
There are 3 types of signal. pre_save/post_save: This signal works before/after the method save(). pre_delete/post_delete: This signal works before after delete a model's instance (method delete()) this signal is thrown. pre_init/post_init: This signal is thrown before/after instantiating a model (__init__() method).
First, to dispel a misconception about signals, they are not executed asynchronously. There is no background thread or worker to execute them. Like most of Django, they are fully "synchronous".
Only use signals to avoid introducing circular dependencies. If you have two apps, and one app wants to trigger behaviour in an app it already knows about, don't use signals. The app should just import the function it needs and call it directly.
As of now, I think that a list of models is the only working way. There is an open discussion about that specific issue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With