views:

422

answers:

2

Hi,

I've got a Python/Django application which runs quite a lot of SQL statements. For debugging purposes, I thought I should create a simple view for me which just lists all the SQL statements that have been run.

According to the documentation, this code should be enough to do that:

    from django.db import connection
    connection.queries

as long as DEBUG is True.

However, this is not giving me anything. DEBUG is most certainly set to True. In what context is this connection.queries stored? I'm mean, I should be able to execute one page which executes a lot of SQL statements, and then just switch over to the http://myserver/sql view I created and see those SQL statements there, right? Using the same browser session of course ...

I did check if db.reset_queries() was being run anywhere in the code, appears it's not.

Any ideas why connection.queries is always empty?

+1  A: 
Ben James
Ok, so I this will only work within each request. That makes sense.Thanks for pointing that out :-)
HaukurHaf
+2  A: 

Ben is right that you only see queries from the current process. You can use it within the same view, or in the console, but not between views.

The best way to see what queries are executing in your views is to use the Django debug toolbar.

Daniel Roseman
I'll check out the django debug toolbar. Thank you.
HaukurHaf