[英]Multiple Inheritance with Peewee Model and QtCore QObject causing metaclass conflict
首先,我是python新手,這是我的第一篇文章,所以,請讓我知道我在這里是否做錯了什么,我會很樂意修復它。
我正在使用Python 2.7.15rc1和Peewee 3.6.4
我要實現的目標是創建一個從peewee的Model以及PySide.QtCore的QObject繼承的類。 像這樣:
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
但是這種配置給我帶來了以下錯誤:
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
如果我嘗試將Model指定為所需的元類(並且我認為可以,因為基本上我只需要QObject的“ is a”關系為true),就可以將其添加到BaseModel中:
__metaclass__ = Model
引發以下錯誤:
AttributeError: 'Model' object has no attribute '_meta'
另外,通過點擊此鏈接 ,我將代碼更改為此:
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
但是元類沖突仍然存在。
我在這附近做錯了什么?
好。 最終,我能夠通過執行以下操作解決問題:
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
即使Eclipse不斷顯示此錯誤,它也能正常工作:
Undefined variable from import: __class__
在這一行:
class Metaclass_Model(Model.__class__):
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.