4
我在Django中使用Django 1.6
和model inheritance
。我想要做的是,吸引新的課程延伸。如何重寫Django中模型的__new__元類方法
這將在Python中做過類似,
class Meta(type):
def __new__(cls, name, bases, newattrs):
do_what_you_want_before()
result= super(Meta, cls).__new__(cls, name, bases, newattrs)
do_what_you_want_after()
return result
class Foo:
__metaclass__ = Meta
class SubFoo(Foo):
pass
當這部分代碼被初始化,定製__new__
方法將被調用。
我該如何在模型繼承的Django中做到這一點。當您嘗試使用Django Models執行此操作時,出現如下錯誤:
TypeError: Error when calling the metaclass bases
metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases
謝謝。