Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django signals: receiver and proxy model?

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.

like image 695
Michael Avatar asked Mar 17 '14 21:03

Michael


People also ask

What is proxy model in Django?

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)

What kind of signals are there in Django?

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

Are Django signals synchronous?

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

Should I use Django signals?

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.


1 Answers

As of now, I think that a list of models is the only working way. There is an open discussion about that specific issue.

like image 126
Germano Avatar answered Oct 06 '22 03:10

Germano