双重继承导致元类冲突 [英] Double inheritance causes metaclass conflict
本文介绍了双重继承导致元类冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个模型类 MenuItem ,我想要扩展TranslatableModel和MPTTModel,如下所示:
class MenuItem(TranslatableModel, MPTTModel):
但它会导致元类冲突:
$ type $:$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $的所有基础)
这个问题是什么解决方案?我希望我可以使用双重继承。
解决方案
您可能需要执行以下操作:
class CombinedMeta(TranslatableModel .__ metaclass__,MPTTModel .__ metaclass__):
pass
class MenuItem(TranslatableModel,MPTTModel):
__metaclass__ = CombinedMeta
这应该给你准确的你想要的,没有任何错误的记录。
I use two django packages - django-mptt (utilities for implementing Modified Preorder Tree Traversal) and django-hvad (model translation).
I have a model class MenuItem and I want to it extends TranslatableModel and MPTTModel, like this:
class MenuItem(TranslatableModel, MPTTModel):
but it causes metaclass conflict:
(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)
What is the solution this problem? I hope that I can use double inheritance.
解决方案
You might want to do the following:
class CombinedMeta(TranslatableModel.__metaclass__, MPTTModel.__metaclass__):
pass
class MenuItem(TranslatableModel, MPTTModel):
__metaclass__=CombinedMeta
This should give you exactly what you want, without any error mesages.
这篇关于双重继承导致元类冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文