Skip to content
How to Reveal the Exact SQL Behind Django ORM Queries

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.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations

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).

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendPythonDjangoORMDebug ToolbarSQL debugging
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.