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"]
# tests.py
@pytest.mark.nplus1
class TestMyView:
    def test_list_books(self, books):
        response = client.get("/books/")  # raises NPlus1Error if view has N+1

Tests marked with @pytest.mark.nplus1 will fail if the code under test triggers an N+1 query. Fix the N+1, or use nplus1_allow() in helper functions that intentionally defer prefetching to their callers.

See examples/ for a working project and the docs for full configuration.

Celery Integration

Detect N+1 queries inside Celery tasks, using the same per-execution scoping as the HTTP middleware.

pip install django-nplus1[celery]
# settings.py
NPLUS1_CELERY = True

Each task execution gets its own detection scope. 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.2.0.tar.gz (15.6 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.2.0-py3-none-any.whl (19.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: django_nplus1-0.2.0.tar.gz
  • Upload date:
  • Size: 15.6 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.2.0.tar.gz
Algorithm Hash digest
SHA256 e462cd1d22fce01ff0db029c6de15a3fc5f8c35d898b768ad26d7f803fb2ac19
MD5 166c290871d07ffb4c73bdb931ebde56
BLAKE2b-256 f336e1fecb9d65a9e0963c9cabdb063fb545b2b447b9f141bf4f75d22c39872a

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_nplus1-0.2.0.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.2.0-py3-none-any.whl.

File metadata

  • Download URL: django_nplus1-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 19.7 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.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 22895101b0e8a8587b970e8b2d92887a1dad39b4d20b77a2d6ff092072be5ac7
MD5 6c92a97b3284a072c72a7b1865430380
BLAKE2b-256 90a9726e2bd9f5aa7b79f75e222c93a44deef020bd83c398d49f2567b2f1f203

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_nplus1-0.2.0-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