1

First of all I'm new to python and that is my first post so, please, let me know if I did something wrong around here and I'll gladly fix it.

I'm using Python 2.7.15rc1 and Peewee 3.6.4

What I'm trying to achieve is create a class that inherits from peewee's Model and also from PySide.QtCore's QObject. Just like this:

class BaseModel(Model, QObject):

id = PrimaryKeyField()

class Meta:
    database = db  

def __str__(self):
    return str(self.__dict__)

def __eq__(self, other): 
    return self.id == other.id

But this configuration brings me the following error:

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

If I try to specify Model as the desired metaclass (and I think it is ok because basically I only need the "is a" relation with QObject to be true) by adding this to BaseModel:

__metaclass__ = Model

The following error is thrown:

AttributeError: 'Model' object has no attribute '_meta'

also, by following this link, I've changed code to this:

class A (Model):
    pass
class B (QObject):
    pass
class C(A, B):
    pass

class BaseModel(A, B):

    __metaclass__ = C

    id = PrimaryKeyField()

    class Meta:
        database = db  

    def __str__(self):
        return str(self.__dict__)

    def __eq__(self, other): 
        return self.id == other.id

But the metaclass conflict persists.

What am I doing wrong around here?

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
wintermute
  • 61
  • 4

1 Answers1

0

Ok. Finally I was able to solve it by doing this:

class Metaclass_Model(Model.__class__):
    pass
class Metaclass_QObject(QObject.__class__):
    pass

class MultiMetaclass(Metaclass_Model, Metaclass_QObject):
    pass


class BaseModel(Model, QObject):

    __metaclass__ = MultiMetaclass

    id = PrimaryKeyField()

    class Meta:
        database = db  

    def __str__(self):
        return str(self.__dict__)

    def __eq__(self, other): 
        return other is not None and self.id == other.id

It works fine, even though Eclipse keeps showing this error:

Undefined variable from import: __class__

at this line:

class Metaclass_Model(Model.__class__):
wintermute
  • 61
  • 4