How to Reveal the Exact SQL Behind Django ORM Queries
This guide shows three practical ways—using the queryset query attribute, Django's connection object, and the Django Debug Toolbar—to inspect the raw SQL generated by Django ORM, helping developers optimize database performance and troubleshoot queries effectively.
While Django ORM simplifies database access, understanding the underlying SQL can greatly improve query performance. Below are three useful methods to view the SQL statements Django executes.
1. Use the queryset’s query attribute
This is the simplest way to see the raw SQL for a queryset.
>> queryset = Organization.objects.all()
>>> print(queryset.query)
SELECT "app_organization"."id", "app_organization"."name", "app_organization"."created", "app_organization"."updated" FROM "app_organization"
>>> str(queryset.query)
'SELECT "app_organization"."id", "app_organization"."name", "app_organization"."created", "app_organization"."updated" FROM "app_organization"'2. Use Django connection
This method provides the executed SQL and the time taken (in seconds). Ensure DEBUG = True in settings.py.
>> from django.db import connection, reset_queries
>>> Organization.objects.all()
>>> connection.queries
[{'sql': 'SELECT "app_organization"."id", "app_organization"."name", "app_organization"."created", "app_organization"."updated" FROM "app_organization" LIMIT 21', 'time': '0.001'}]
>>> reset_queries()
[]Note: reset_queries() can be called at any time to clear the query list.
3. Django Debug Toolbar
The Debug Toolbar displays detailed request/response information, including all SQL queries. Install it with: pip install django-debug-toolbar Then configure your project:
# settings.py
DEBUG = True
INSTALLED_APPS = [
'debug_toolbar',
# ... other apps
]
MIDDLEWARE = [
# ... other middleware
'debug_toolbar.middleware.DebugToolbarMiddleware',
]
INTERNAL_IPS = [
'127.0.0.1',
]Add the toolbar URLs to your URL configuration:
import debug_toolbar
from django.conf import settings
from django.urls import include, path
urlpatterns = [
# ... existing patterns
path('__debug__/', include(debug_toolbar.urls)),
]When enabled, the toolbar appears in the browser and shows each SQL query executed for the current request.
Article originally from Python运维技术 (copyright belongs to the original author).
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
