Last active
May 16, 2025 10:24
-
-
Save rixx/e64a074445ec8adb7ddf18d35c06e6a5 to your computer and use it in GitHub Desktop.
Django: Start debugger on query
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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