TypeError: Cannot create a consistent MRO for bases Mixin, object
TypeError: Cannot create a consistent MRO for bases Mixin, object
我正在尝试向 Django QuerySet 添加一个 explain() 方法,但收到 MRO 错误:
File ".../mixin.py", line 10, in <module>
QuerySet.__bases__ += (QuerySetExplainMixin,)
TypeError: Cannot create a consistent method resolution
order (MRO) for bases QuerySetExplainMixin, object
mixin 和我添加的代码:
from django.db import connections
from django.db.models.query import QuerySet
class QuerySetExplainMixin:
def explain(self):
cursor = connections[self.db].cursor()
query, params = self.query.sql_with_params()
cursor.execute('explain %s' % query, params)
return '\n'.join(r[0] for r in cursor.fetchall())
QuerySet.__bases__ += (QuerySetExplainMixin,)
(mixin 的致谢:)
此代码是为 Python 编写的 2,这就是为什么您在 Python 中遇到冲突的原因 3,除了下面我看不到在 QuerySet
上设置新方法的其他方法对象:
from django.db import connections
from django.db.models.query import QuerySet
def explain(self):
cursor = connections[self.db].cursor()
query, params = self.query.sql_with_params()
cursor.execute('explain %s' % query, params)
return '\n'.join(r[0] for r in cursor.fetchall())
type.__setattr__(QuerySet, 'explain', explain)
希望对您有所帮助
我正在尝试向 Django QuerySet 添加一个 explain() 方法,但收到 MRO 错误:
File ".../mixin.py", line 10, in <module>
QuerySet.__bases__ += (QuerySetExplainMixin,)
TypeError: Cannot create a consistent method resolution
order (MRO) for bases QuerySetExplainMixin, object
mixin 和我添加的代码:
from django.db import connections
from django.db.models.query import QuerySet
class QuerySetExplainMixin:
def explain(self):
cursor = connections[self.db].cursor()
query, params = self.query.sql_with_params()
cursor.execute('explain %s' % query, params)
return '\n'.join(r[0] for r in cursor.fetchall())
QuerySet.__bases__ += (QuerySetExplainMixin,)
(mixin 的致谢:)
此代码是为 Python 编写的 2,这就是为什么您在 Python 中遇到冲突的原因 3,除了下面我看不到在 QuerySet
上设置新方法的其他方法对象:
from django.db import connections
from django.db.models.query import QuerySet
def explain(self):
cursor = connections[self.db].cursor()
query, params = self.query.sql_with_params()
cursor.execute('explain %s' % query, params)
return '\n'.join(r[0] for r in cursor.fetchall())
type.__setattr__(QuerySet, 'explain', explain)
希望对您有所帮助