Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Double inheritance causes metaclass conflict

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.

like image 364
David Silva Avatar asked Jun 29 '12 00:06

David Silva


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 error mesages.

like image 116
schacki Avatar answered Oct 19 '22 07:10

schacki


Sorry for the late answer, but it i think it'll help for people who have same problem. When you subclass MPTTModel and another class, make sure you put MPTTModel first, like this:

class MenuItem(MPTTModel, TranslatableModel):
like image 36
Otskimanot Sqilal Avatar answered Oct 19 '22 06:10

Otskimanot Sqilal


Generally the answer of @schacki would work. However, django-hvad overrides many other manager/queryset classes under the hood, which makes integrating with django-mptt/django-polymorphic and friends impossible at the moment.

Take a look at django-parler, which features a similar API and admin integration as django-hvad, but plays nice with other packages too. The table layout is identical, so you should be able to switch the packages easily.

like image 1
vdboor Avatar answered Oct 19 '22 06:10

vdboor