Skip to main content

N+1 query detection for Django

Project description

django-nplus1

N+1 query detection for Django. Beta - API may still change before 1.0.

Quick Start

pip install django-nplus1
# settings.py
INSTALLED_APPS = [..., "django_nplus1"]
# settings/testing.py
MIDDLEWARE = [..., "django_nplus1.NPlus1Middleware"]
NPLUS1_RAISE = True

Adding the middleware to your test settings means every view test that goes through the Django test client will fail on N+1 queries. This catches real problems in actual request paths without false positives from helper functions or scripts that intentionally defer prefetching.

For existing projects, introducing django-nplus1 will likely surface many N+1 queries at once. Whitelist the known issues and fix them over time:

# settings/testing.py
NPLUS1_WHITELIST = [
    {"model": "myapp.Author", "field": "books"},
    {"model": "myapp.Book", "field": "publisher"},
]

The middleware can also run in development or production settings to log warnings instead of raising — see the docs for all options, including the pytest plugin and the Profiler context manager.

See examples/ for a working project.

Celery Integration

The equivalent of the middleware for Celery tasks — each task execution gets its own detection scope.

pip install django-nplus1[celery]
# settings.py (or settings/testing.py)
NPLUS1_CELERY = True

Lazy loads, .get()-in-a-loop, unused eager loads, and duplicate queries are all detected per-task, just as they are per-request. nplus1_allow() works inside tasks the same way it does in views.

Limitations:

  • nplus1_allow() context does not propagate across task boundaries. If a view calls task.delay() inside an nplus1_allow() block, the allow rules do not carry into the worker (ContextVars don't survive serialization).
  • Scope nesting for synchronous subtasks (.apply() inside a task) creates a separate scope for the inner task.

Credits

This project builds on the work of:

  • nplusone by Joshua Carp - the original automatic N+1 detection library for Python ORMs. django-nplus1 started as a Django-specific fork of nplusone's architecture.
  • django-zeal by Tao Bojlen - inspired several features including deferred field detection, .get()-in-a-loop detection, ContextVar-based async safety, call-site tracking, and configurable thresholds.

License

MIT

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_nplus1-0.3.5.tar.gz (17.1 kB view details)

Uploaded Source

Built Distribution

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

django_nplus1-0.3.5-py3-none-any.whl (21.2 kB view details)

Uploaded Python 3

File details

Details for the file django_nplus1-0.3.5.tar.gz.

File metadata

  • Download URL: django_nplus1-0.3.5.tar.gz
  • Upload date:
  • Size: 17.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for django_nplus1-0.3.5.tar.gz
Algorithm Hash digest
SHA256 6c48fa11d35fbb6553afa82065b1a8234b2104a1e720c9770f20e8c4be838506
MD5 a74e79d4d091d2296d365ea87ba6c03c
BLAKE2b-256 255dd48fc51e30a6ef2b0a371fd1283ed8ad16a5297a34463209db456c26367d

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_nplus1-0.3.5.tar.gz:

Publisher: publish.yml on oliverhaas/django-nplus1

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file django_nplus1-0.3.5-py3-none-any.whl.

File metadata

  • Download URL: django_nplus1-0.3.5-py3-none-any.whl
  • Upload date:
  • Size: 21.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for django_nplus1-0.3.5-py3-none-any.whl
Algorithm Hash digest
SHA256 7ee3d54c8d3684fe96a73f3e51e0bca460a493eb7768f554bdd21ae68f6511c2
MD5 b9690d3d568d174416258fc8aa907c5d
BLAKE2b-256 423fa49c652780b67ac673c8b143a8b51a36e7b280ae194d6ad1db19a79b33cd

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_nplus1-0.3.5-py3-none-any.whl:

Publisher: publish.yml on oliverhaas/django-nplus1

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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