3👍
✅
This code was written for Python 2, that’s why you get a conflict in Python 3, i cannot see another way than below to set the new method on the QuerySet
object :
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)
I hope this can help you.
Source:stackexchange.com