Skip to main content

Lightweight Django query performance monitoring and debugging toolkit

Project description

django-query-watch

Lightweight Django query performance monitoring and debugging toolkit.

PyPI version Python License: MIT CI


What is this?

django-query-watch is a developer tool that monitors your Django application's database queries in real time — directly in your terminal.

It helps you catch:

  • Slow queries exceeding a configurable threshold
  • Duplicate queries caused by N+1 problems
  • Per-request summaries showing total query count and execution time

No dashboards. No admin panels. Just clean, readable terminal output while you develop.


Features

  • Slow query detection with configurable threshold
  • Duplicate / N+1 query detection
  • Per-request query summary
  • Rich colored terminal output
  • Zero frontend dependencies
  • Minimal overhead
  • Works with PostgreSQL, MySQL, and SQLite

Installation

pip install django-query-watch

Quick Start

1. Add to INSTALLED_APPS:

INSTALLED_APPS = [
    ...
    "django_query_watch",
]

2. Add middleware:

MIDDLEWARE = [
    ...
    "django_query_watch.middleware.QueryWatchMiddleware",
]

3. Run your server:

python manage.py runserver

That's it. Query monitoring starts automatically.


Configuration

Add this to your settings.py to customize behavior:

DJANGO_QUERY_WATCH = {
    "ENABLED": True,
    "SLOW_QUERY_THRESHOLD_MS": 200,
    "LOG_DUPLICATE_QUERIES": True,
    "LOG_QUERY_SUMMARY": True,
    "MAX_QUERY_PREVIEW_LENGTH": 500,
}
Setting Default Description
ENABLED True Enable or disable the package
SLOW_QUERY_THRESHOLD_MS 200 Threshold in ms to flag slow queries
LOG_DUPLICATE_QUERIES True Detect and log duplicate queries
LOG_QUERY_SUMMARY True Print per-request summary
MAX_QUERY_PREVIEW_LENGTH 500 Max SQL characters shown in logs

Example Output

Slow query detected:

╭─ [DJANGO-QUERY-WATCH] ⚠ Slow Query Detected ────────────────╮
│                                                               │
│  Execution Time: 423ms                                        │
│  Request:        /api/orders/                                 │
│                                                               │
│  SQL:                                                         │
│  SELECT * FROM shop_product WHERE ...                         │
│                                                               │
╰───────────────────────────────────────────────────────────────╯

Duplicate query detected (N+1):

╭─ [DJANGO-QUERY-WATCH] ⚠ Duplicate Query Detected ───────────╮
│                                                               │
│  Repeated: 18 times                                           │
│                                                               │
│  SQL:                                                         │
│  SELECT * FROM shop_category WHERE id = 1                     │
│                                                               │
╰───────────────────────────────────────────────────────────────╯

Request summary:

╭─ [DJANGO-QUERY-WATCH] ✔ Request Summary ────────────────────╮
│                                                               │
│  Path:              /api/products/                            │
│  Total Queries:     1                                         │
│  Total Time:        1.0ms                                     │
│  Slow Queries:      0                                         │
│  Duplicate Queries: 0                                         │
│                                                               │
╰───────────────────────────────────────────────────────────────╯

Common Use Cases

Catching N+1 queries:

# Bad — triggers N+1
for product in Product.objects.all():
    print(product.category.name)  # query per product

# Good — single query
for product in Product.objects.select_related("category").all():
    print(product.category.name)

django-query-watch will flag the first version with duplicate query warnings and show you exactly which SQL is repeating.


Why this exists

Django's ORM makes it easy to write queries that look clean but perform badly at scale. Tools like Django Debug Toolbar are great but require a browser and add significant setup. django-query-watch gives you instant terminal feedback with zero friction — just add the middleware and start developing.


Requirements

  • Python 3.10+
  • Django 4.0+
  • Works with DEBUG = True (development only)

Roadmap

  • Query export to JSON/CSV
  • OpenTelemetry integration
  • Async support
  • Prometheus metrics
  • Optional admin dashboard

Contributing

Pull requests are welcome. For major changes please open an issue first.

git clone https://github.com/harinis05122001/django-query-watch
cd django-query-watch
python -m venv venv
source venv/bin/activate
pip install -e .
pytest tests/ -v

License

MIT License. See LICENSE for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_query_watch-1.0.0.tar.gz (9.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_query_watch-1.0.0-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

File details

Details for the file django_query_watch-1.0.0.tar.gz.

File metadata

  • Download URL: django_query_watch-1.0.0.tar.gz
  • Upload date:
  • Size: 9.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for django_query_watch-1.0.0.tar.gz
Algorithm Hash digest
SHA256 f973385610e705234f2b37a006521749ef5928740621467a6398c9ec8a9a63eb
MD5 9128e8e64426c45dc1dc336363e959ac
BLAKE2b-256 42237831bdf5e882e979827f5e9f24f1db844c1a448220061af5fe8cedd4e1d9

See more details on using hashes here.

File details

Details for the file django_query_watch-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_query_watch-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 981dacb493805fe736a7b76f7637e0d10ecfff59e48270f71f6e521ead5eb40a
MD5 f5faee7a04fbd4d0d33460498b14537d
BLAKE2b-256 0e6a38d7c0ac8eaeed710308f3751159533e9f5df4d434ffd126bd6052015a16

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page