Is there a way to print the number of raw SQL queries performed by the Django ORM during a Django shell session?
This sort of information is already provided by the Django debug toolbar (e.g, 5 QUERIES in 5.83MS
but it's not obvious how to get it from the shell.
This is a slight improvement on the accepted answer. create a python file named extra_imports.py in some app (Eg
some_app
)extra_imports.py
Now, If you are using shell_plus from django_extension (btw check it out if you are not using it), add following line to settings.py
If you are using django shell, run this inside shell
Now,
If you have database routing and multiple connections, it's a bit trickier to count your database hits because
connection.queries
considers only the default connection, as far as I can tell.To include all connections:
or more concisely:
reset_queries()
already handles multiple connectionsYou can use
connection.queries
: