Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using django.db.connection.queries

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?

like image 410
HaukurHaf Avatar asked Jan 25 '10 16:01

HaukurHaf


People also ask

How does Django handle database connections?

Django opens a connection to the database when it first makes a database query. It keeps this connection open and reuses it in subsequent requests. Django closes the connection once it exceeds the maximum age defined by CONN_MAX_AGE or when it isn't usable any longer.

What are connection queries in Django?

connection. queries includes all SQL statements – INSERTs, UPDATES, SELECTs, etc. Each time your app hits the database, the query will be recorded.

Can we write SQL queries in Django?

Django gives you two ways of performing raw SQL queries: you can use Manager. raw() to perform raw queries and return model instances, or you can avoid the model layer entirely and execute custom SQL directly. Explore the ORM before using raw SQL!


2 Answers

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.

like image 191
Daniel Roseman Avatar answered Sep 20 '22 12:09

Daniel Roseman


@Daniel Roseman its a good idea, but if you want to know sql queries out of the box:

install django-command-extensions and add it to installed apps. it will add many utils commands into your project, one of them:

  • debugsqlshell: Outputs the SQL that gets executed as you work in the Python interactive shell.

example: python manage.py debugsqlshell

In [1]:from django.contrib.auth.models import User
In [1]:User.objects.all()

Out[2]: SELECT "auth_user"."id",
   "auth_user"."username",
   "auth_user"."first_name",
   "auth_user"."last_name",
   "auth_user"."email",
   "auth_user"."password",
   "auth_user"."is_staff",
   "auth_user"."is_active",
   "auth_user"."is_superuser",
   "auth_user"."last_login",
   "auth_user"."date_joined"
    FROM "auth_user" LIMIT 21  [1.25ms]
like image 37
panchicore Avatar answered Sep 20 '22 12:09

panchicore