如何在Django中覆盖模型的__new__元类方法

Ahm*_*DAL 4 python django

我正在Django 1.6model inheritanceDjango一起使用.我想做的是,挂钩新课程.

它将在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
Run Code Online (Sandbox Code Playgroud)

初始化此部分代码时,__new__将调用自定义方法.

我怎么能在模型继承的Django中做到这一点.当您尝试使用Django模型执行此操作时,会出现以下错误:

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
Run Code Online (Sandbox Code Playgroud)

谢谢.

Mac*_*Gol 7

而不是继承type,继承自django.db.models.base.ModelBase(来源).