Can Django pre_save work for all derived classes

I have an Action model class that extends with several other classes. I am new to django and suggested that if I call pre_save.connect (actionFunc, sender = Action), then actionFunc will be called at any time when the save method was called in the Action class (including any derived class).

My observation is that this function only runs when the instance is a direct match of the type of the class defined in Sender. Is there a way to get this to receive signals for all derived Action instances?

+3
source share
2 answers

No, you have to call pre_save.connectso many times.

However, you can use python to get all classes that extend the class of your interests and iterate over the pre_save connect statement.

Say if extended classes Actionare in a specific file, you can do the following:

global_dict = globals().copy()
[el for el in global_dict.values() if getattr(el,'__base__',None)==Action]
+1
source

one thing you can do is change the sender of the signal in django so that instead of matching with a specific type instead

if isinstance(sender, filter):
    send_signal()

(pseudo code)

0
source

Source: https://habr.com/ru/post/1758549/


All Articles