Is it possible to perform raw queries in django rest framework like django. https://docs.djangoproject.com/en/dev/topics/db/sql/#performing-raw-queries
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Yes you should be able to, since you can customize the queryset that backs your view, e.g.
class MyModelViewSet(viewsets.ModelViewSet):
# The usual stuff here
model = MyModel
def list(self, request):
queryset = MyModel.objects.raw('... your SQL here...')
serializer = MyModelSerializer(queryset, many=True)
return Response(serializer.data)
Manager.raw()
returns RawQuerySet
which is a QuerySet
, so you can see how it all fits