Zero-UI middleware for analyzing Django SQL performance right in your terminal or API responses.
π Full Documentation (Wiki)
- Catch performance issues early during development.
- Pinpoint the exact Python line causing N+1 or duplicate queries.
- API-native & CI-friendly β no browser toolbar needed.
- Lightweight and focused on SQL performance.
- Zero Data Leakage: 100% Thread-Safe, ensuring metrics stay isolated per request.
While Django Debug Toolbar is a great visual tool, QueryGuard follows a different philosophy for modern backend workflows:
| Feature | Django Debug Toolbar | QueryGuard |
|---|---|---|
| UI | Browser sidebar | Terminal + HTTP headers |
| APIs / JSON | Awkward | Native support |
| Culprit detection | Manual inspection | Automatic file + line reporting |
| CI/CD integration | Hard to automate | Easy to plug into tests / pipelines |
QueryGuard is built for modern, high-traffic Django environments (Gunicorn, uWSGI, Daphne).
By leveraging asgiref.local.Local (or threading.local as fallback), it guarantees that:
- Metrics remain isolated: SQL counts for User A will never leak into the headers for User B.
- No Global State: Data is strictly scoped to the lifecycle of a single HTTP request.
- Minimal Overhead: The monitoring logic is lightweight and thread-contained
# Clone & setup
pip install uv # optional for fast dependency handling
git clone https://github.com/addonol/django-query-guard
cd django-query-guard
uv sync --all-extras --dev # or your usual virtualenv + pipEnable the middleware in settings.py:
MIDDLEWARE = [
# ...
"query_guard.middleware.SQLPerformanceMiddleware",
]Run your server:
python manage.py runserverMake a request and watch your terminal for QueryGuard output.
==================== EXECUTING VIEW ====================
08:34:31 [WARNING] query_guard:
βββββββββββββββββββββββββββββββββββββββββββββββββ
β QUERYGUARD REPORT β
βββββββββββββββββββββββββββββββββββββββββββββββββ€
β Status : DUPLICATE SQL β
β Path : /demo-url/ β
βββββββββββββββββββββββββββββββββββββββββββββββββ€
β Queries : 3 β
β Duplicates : 1 β
β Duration : 0.0090s β
β Culprit : demo.py:111 (demo_view) β
βββββββββββββββββββββββββββββββββββββββββββββββββ
==================== INJECTED HEADERS ======================
X-QueryGuard-Count: 3
X-QueryGuard-Duplicates: 1
X-QueryGuard-Culprit: demo.py:111 (demo_view)
============================================================
Typical response headers:
- X-QueryGuard-Count
- X-QueryGuard-Duplicates
- X-QueryGuard-Culprit
You can inspect these via browser dev tools, curl -i, Postman, or in your Django tests.
| Section | Description |
|---|---|
| Home | Overview, philosophy, key concepts |
| Getting Started | Installation and basic setup |
| Configuration | Settings and thresholds |
| Usage & Examples | N+1 and duplicate query examples |
| Development & Contributing | Local setup, tests, contribution guidelines |
| Roadmap & FAQ | Future plans and common questions |
python demo.pyContributions are welcome!
- Read the Development & Contributing guide.
- Open issues here: https://github.com/addonol/django-query-guard/issues.
This project is licensed under the MIT License. See the LICENSE file for details.