Double inheritance causes metaclass conflict

I use two django packages - django-mptt (utilities for implementing a modified traversal of the order tree) and django-hvad (model translation).

I have a MenuItem model class and I want it to extend the TranslitableModel and MPTTModel, for example:

class MenuItem(TranslatableModel, MPTTModel): 

but this causes a 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 to this problem? I hope that I can use double inheritance.

+7
source share
3 answers

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 errors.

+5
source

Sorry for the late reply, but I think this will help people who have the same problems. When you subclass MPTTModel and another class, first put the MPTTModel as follows:

 class MenuItem(MPTTModel, TranslatableModel): 
+2
source

Generally, @schacki's answer will work. However, django-hvad overrides many other manager / request classes under the hood, which makes integration with django-mptt / django-polymorphic and friends currently impossible.

Take a look at django-parler , which implements a similar integration of APIs and admins as django-hvad, but goes well with other packages. The table layout is identical, so you can easily switch packages.

+1
source

All Articles