tags:

views:

31

answers:

3

Guys, How can i see my django queries from manage shell interface

I have tried using this but gives me queries that pass through the django server

from django.db import connection
connection.queries()

I have seen it somewhere, can't remember where??

help

Gath

A: 

I use this Django code snippet that lists all the queries in a view in the rendered template. Pretty useful.

rubayeet
A: 

You can print out the sql for individual queries like so:

your_query = YourModel.objects.all()
print your_query.query

Is that all you need?

sdolan
Does it show the raw SQl statement?
gath
@gath: Yes it does.
sdolan
+2  A: 

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.

Manoj Govindan
Ok, what i was doing was assigning the queryset to a variable e.g cur = Entry.objects.all(). But am ok now. thanks
gath