为什么装饰器外的相同代码工作而不是内部?

时间:2014-02-28 16:50:01

标签: python django django-models

models.py中的此代码有效:

f = auto_history()
f.history_connect_signals(Product, Product_History)

但是这段代码不起作用,实际上代码没有做任何事情:

def auto_signals(_sender):
    def history_signals(_history):
        print str(_sender)+str(_history)
        f = auto_history()
        f.history_connect_signals(_sender, _history)
        return _history
    return history_signals

这是实例化的类:

class auto_history(object):
    def class_save(self, sender, instance, signal,created=False, *args, **kwargs):
        h = self.history_class()
        h.save_history(_signal=signal, _created=created, _instance=instance)

    def history_connect_signals(self, _product, _history):
        self.history_class = _history
        post_save.connect(self.class_save, sender=_product)    
        pre_delete.connect(self.class_save, sender=_product)

    def __call__(self, _sender, _history):
        self.history_connect_signals(_sender, _history)

我使用这个装饰器将模型与作为发送者的类连接到auto_history类以保存任何更改。

@auto_signals(Product)
class Product_History(Model_History, ProductBase):

0 个答案:

没有答案