3

我有一个模型类“Action”,它被其他几个类扩展。我是 django 的新手,并假设如果我调用 pre_save.connect(actionFunc, sender=Action),那么只要调用 Action 类中的 save 方法(包括任何派生类)都会调用 actionFunc。

我的观察是,只有当实例与 Sender 中定义的 Class 类型直接匹配时才会触发此函数。有没有办法让这个也接收所有派生的 Action 实例的信号?

4

2 回答 2

1

No, you have to call the pre_save.connect as many number of times.

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

Say, if the extended classes of the Action are all in a given file, you can do the following:

global_dict = globals().copy()
[el for el in global_dict.values() if getattr(el,'__base__',None)==Action]
于 2010-08-07T21:28:06.443 回答
0

您可以做的一件事是修改 django 中的信号发送器,以便与其匹配特定类型而不是匹配

if isinstance(sender, filter):
    send_signal()

(伪代码)

于 2010-08-11T07:39:38.593 回答