Skip to main content

Django QueryWatch

Automated database-performance analysis for Django.

Source code and issue tracking: Atiqumer/django-querywatch.

QueryWatch observes database executions made by a Django request or test, groups query patterns, and turns suspicious behavior into explainable findings. It is a local-development and test/CI tool; it does not modify queries, create database tables, or send data to an external service.

Status: pre-alpha. The public API may change before 1.0.

Why QueryWatch?

Query viewers show what ran. QueryWatch focuses on what may deserve attention:

  • queries at or above a configurable duration threshold;
  • repeated query patterns;
  • possible N+1 patterns with distinct parameters; and
  • query budgets for tests.

Findings are rule-based and intentionally conservative. In particular, an N+1 finding is described as possible because SQL alone cannot prove which Python source line or Django relationship caused it.

Compatibility

QueryWatch targets Python 3.12–3.14 and Django 5.2–6.1. SQLite is used for the current test suite. PostgreSQL and MySQL compatibility verification is planned before a stable release.

Installation

After the package is published to PyPI:

python -m pip install django-query-sentinel

For development from a checkout, see Development.

Quick start

# settings.py

INSTALLED_APPS = [
    # ...
    "querywatch",
]

MIDDLEWARE = [
    # ...
    "querywatch.middleware.QueryWatchMiddleware",
]

The middleware logs a console report through the querywatch logger after each synchronous request. Set QUERYWATCH_OUTPUT = "none" when you only want to use the testing API.

Configuration

# settings.py

QUERYWATCH_ENABLED = True
QUERYWATCH_OUTPUT = "console"  # "console", "json", or "none"

QUERYWATCH_SLOW_QUERY_MS = 100
QUERYWATCH_DUPLICATE_THRESHOLD = 3
QUERYWATCH_NPLUS1_THRESHOLD = 3
QUERYWATCH_MAX_QUERIES = None  # Request-level finding; disabled

QUERYWATCH_MAX_QUERIES causes an error-level finding in request reports. It does not change an HTTP response. To fail a test, use assert_queries().

Reports

Console output is intentionally dependency-free:

QueryWatch
────────────────────────────────
Queries: 27
Total DB time: 84.3ms
Slow queries: 2
Duplicate groups: 4

Findings
────────────────────────────────
WARNING  Possible N+1 query pattern
A query pattern was executed repeatedly with different parameters.

Set QUERYWATCH_OUTPUT = "json" to log a machine-readable JSON object with a summary and findings. Reports include structural evidence, timing values, and fingerprints; they do not include query parameters.

Query budgets

from querywatch.testing import assert_queries


def test_dashboard(client):
    with assert_queries(max_queries=10):
        response = client.get("/dashboard/")

    assert response.status_code == 200

If the budget is exceeded, QueryWatchError exposes actual_queries, max_queries, exceeded_by, findings, and report for programmatic assertions.

Detectors

Detector Default Severity
Slow query QUERYWATCH_SLOW_QUERY_MS = 100 warning
Duplicate query QUERYWATCH_DUPLICATE_THRESHOLD = 3 info
Possible N+1 QUERYWATCH_NPLUS1_THRESHOLD = 3 warning
Query count disabled (QUERYWATCH_MAX_QUERIES = None) error

Privacy and safety

  • Captured query data exists only in memory for the active scope.
  • QueryWatch does not create models, migrations, caches, queues, or services.
  • QueryWatch does not re-execute captured SQL and never runs EXPLAIN ANALYZE.
  • Query parameters are not included in console or JSON reports.
  • QueryWatch does not change a QuerySet, transaction, SQL statement, or result.

Limitations

  • Middleware instrumentation is synchronous. Native async instrumentation is not claimed in v0.1.
  • SQL normalization is deliberately conservative; comments and PostgreSQL dollar-quoted strings are left unchanged.
  • Possible-N+1 detection is heuristic and does not infer exact relationship names or source locations.
  • This is not a production-monitoring service or a historical metrics store.

Development

Requires Python 3.12+.

python -m pip install -e ".[dev]"
python -m ruff check .
python -m pytest

Run the complete test suite before submitting a change. Tests use an isolated SQLite Django test database and require no network access.

Packaging verification

Build both distribution formats locally:

python -m build

This creates a wheel and source distribution in dist/. Verify the wheel in a fresh virtual environment before publishing:

python -m venv .wheel-test
.\.wheel-test\Scripts\python -m pip install .\dist\django_query_sentinel-0.1.1-py3-none-any.whl
.\.wheel-test\Scripts\python -c "import querywatch; print(querywatch.__name__)"

Replace the wheel filename if the package version changes. Do not publish until the wheel installs and imports successfully.

For the tag-based Trusted Publishing release process, see RELEASING.md.

Contributing

See CONTRIBUTING.md. Please keep changes small, add tests for behavior changes, and avoid new runtime dependencies unless there is a concrete need.

License

QueryWatch is released under the MIT License.

Metadata

Release files for django-query-sentinel 0.1.1

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

Source distribution (sdist)

Source distribution for django-query-sentinel 0.1.1
File Size Uploaded Provenance Actions
django_query_sentinel-0.1.1.tar.gz 29.5 kB With provenance Details for django_query_sentinel-0.1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for django-query-sentinel 0.1.1
File Interpreter ABI Platform Actions
django_query_sentinel-0.1.1-py3-none-any.whl Python 3 none any Details for django_query_sentinel-0.1.1-py3-none-any.whl

Total release size: 47.0 kB

Release files / django_query_sentinel-0.1.1.tar.gz

Download URL django_query_sentinel-0.1.1.tar.gz
Size 29.5 kB
Tags Source
SHA-256 checksum
How to use checksums
33b7861f0901d4804c51a43ef02c85239e947b91915148cf3fe49f14c855c82a
BLAKE2b-256 checksum
How to use checksums
07b190d346d4e4f01d0bbbd4c0a2d886a437a6add49e39e2e61df106169f239a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 26, 2026.

Transparency log

Release files / django_query_sentinel-0.1.1-py3-none-any.whl

Download URL django_query_sentinel-0.1.1-py3-none-any.whl
Size 17.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
559ac021013993cf1c198d7f603cd1af5038b49cac5f1330ed205856e516abac
BLAKE2b-256 checksum
How to use checksums
fcae12c0a225a5dcf1779c8b1a746c239b09b124abe9256e4b8dc42378538d15
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 26, 2026.

Transparency log

Security for django-query-sentinel 0.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for django_query_sentinel-0.1.1.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 26, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files Full provenance

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