Skip to main content

PyPI version

django-sql-debug

Output in the console the executed SQL of queries made by Django's ORM.

This is a development tool that should only be used in a local environment and never included in production code.

Features

  • Print executed SQL to the console
  • Automatically runs EXPLAIN ANALYSE
  • Allows for debugging of single query, function, test or the entire application

Why not use django-debug-toolbar?

Django Debug Toolbar is great if you are developing an html website using Django. However, since it is focused on displaying the queries using HTML code inject in the webpage it is not suitable if you are developing a REST API or need to view the queries being run in a unit test. This lib was inspired by the weakenesses of Django Debug Toolbar on these use cases.

Installation

pip install django-sql-debug

Examples

There are many ways to use the library. You can set the scope of the debug using one of the examples below.

Debugging a small segment of code

from django_sql_debug import debug_sql


with @debug_sql():
    SomeModel.objects.get(id=5)  # will be captured
SomeModel.objects.get(id=5)  # will not be captured

Debugging a single function or test

from django_sql_debug import debug_sql


class MyTest(TestCase):
    @debug_sql()
    def test_will_show_executed_sql(self):
        SomeModel.objects.get(id=5)

    def test_will_not_show_executed_sql(self):
        SomeModel.objects.create(name='test')

Debugging an entire TestCase

from django_sql_debug import DebugSQLTestCaseMixin


class MyTest(DebugSQLTestCaseMixin, TestCase):
    def test_will_show_executed_sql(self):
        SomeModel.objects.get(id=5)

    def test_will_also_show_executed_sql(self):
        SomeModel.objects.create(name='test')

Debugging an entire application

Add django_sql_debug to the INSTALLED_APPS and MIDDLEWARES.

INSTALLED_APPS = [
    'django_sql_debug',

    ...
]

MIDDLEWARE = [
    'django_sql_debug.DebugSQLMiddleware',

    ...
]

Configuration

You can change some configurations in Django's settings.py

  • SQL_DEBUG_ENABLE_PARAMS: Include the SQL parameters section in the debug log (default: True)
  • SQL_DEBUG_ENABLE_PERFORMANCE: Include the SQL performance section in the debug log (default: True)
  • SQL_DEBUG_ENABLE_EXPLAIN: Run EXPLAIN command on supported databases (default: True)
  • SQL_DEBUG_OUTPUT_STREAM: The output stream to write sql queries and additional sections (default: sys.stdout)
  • SQL_DEBUG_OUTPUT_SIZE: The size of the output headers (default: terminal width)

Metadata

Release files for django-sql-debug 0.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for django-sql-debug 0.0.3
File Interpreter ABI Platform
django_sql_debug-0.0.3-py3-none-any.whl Python 3 none any Details

Release files / django_sql_debug-0.0.3-py3-none-any.whl

Download URL django_sql_debug-0.0.3-py3-none-any.whl
Size 6.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e84ccc6a94b94b7cefdff9353203ab4f49b4c05d123ee9481eb9c3313b0bcee9
BLAKE2b-256 checksum
How to use checksums
50403f0bdc769b625348f1c03e1be8d1757b940c726580d0608fcf2186d930bd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.5

Release history Release notifications | RSS feed

This release

0.0.3 This release

1 release file

0.0.2

1 release file

0.0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page