是否有可能以某种方式将RawQuerySet
转换为QuerySet
或以__and__
方式模拟__or__
QuerySet
方法RawQuerySet
这样我就能得到RawQuerySet
和QuerySet
的联合和交集?
答案 0 :(得分:1)
这link最终帮了我很多。
完整摘录:
from django.db import connection, models
class MyManager(Manager):
def raw_as_qs(self, raw_query, params=()):
"""Execute a raw query and return a QuerySet. The first column in the
result set must be the id field for the model.
:type raw_query: str | unicode
:type params: tuple[T] | dict[str | unicode, T]
:rtype: django.db.models.query.QuerySet
"""
cursor = connection.cursor()
try:
cursor.execute(raw_query, params)
return self.filter(id__in=(x[0] for x in cursor))
finally:
cursor.close()
class MyModel(models.Model):
objects = MyManager()