【发布时间】:2012-06-30 13:23:48
【问题描述】:
我使用两个 django 包 - django-mptt(用于实现修改的预序树遍历的实用程序)和 django-hvad(模型翻译)。
我有一个模型类 MenuItem,我希望它扩展 TranslatableModel 和 MPTTModel,如下所示:
class MenuItem(TranslatableModel, MPTTModel):
但它会导致元类冲突:
(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)
这个问题的解决方案是什么?我希望我可以使用双重继承。
【问题讨论】:
-
为什么需要多重继承?通常最好尽量避免。
-
因为 MenuItem 有翻译(需要扩展 TranslatableModel)并且必须具有树层次结构(需要扩展 MPTTModel)。
-
MPTTModel 具有元类 MPTTModelBase,TranslatableModel 具有元类 TranslatableModelBase。就像@jathanism 链接到的问题中的答案一样,您必须设置元类,因为 Python 不知道要使用哪个基类的元类。
标签: python django database-design