Skip to content

Instantly share code, notes, and snippets.

@rixx
Last active May 16, 2025 10:24
Show Gist options
  • Save rixx/e64a074445ec8adb7ddf18d35c06e6a5 to your computer and use it in GitHub Desktop.
Save rixx/e64a074445ec8adb7ddf18d35c06e6a5 to your computer and use it in GitHub Desktop.
Django: Start debugger on query
# Use: wrap methods:
# @breakpoint_on_query()
# Or use as `with breakpoint_on_query():`
from contextlib import contextmanager
@contextmanager
def breakpoint_on_query():
from django.db import connection
def instrument(execute, sql, params, many, context):
# Optional: match only specific queries, e.g.
# if sql.startswith("DELETE"), or "foo_table" in sql
breakpoint()
return execute(sql, params, many, context)
with connection.execute_wrapper(instrument):
yield
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment