Django how do i view query in manage shell
There are two ways to view the query in the shell. First, if you are using a queryset you can use the query
attribute of the queryset. For e.g.
qs = MyModel.objects.all()
print qs.query
Second when the query is not visible immediately. For e.g. when you are updating a queryset using update()
. In this case you can:
from django.db import connection
MyModel.objects.all().update(foo = 'bar')
print connection.queries
# print connection.queries[-1] # if you want to see only the last query
I have tried using this but gives me queries that pass through the django server
I don't understand what you mean by "gives me queries that pass through the Django server". Are you trying to see the queries while running the application? In that case use the django-debug-toolbar or the snippet referred to by @rubayeet.