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.1.tar.gz (15.5 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.1-py3-none-any.whl (19.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: django_nplus1-0.2.1.tar.gz
  • Upload date:
  • Size: 15.5 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.1.tar.gz
Algorithm Hash digest
SHA256 bebc1b5bae570d93fc1b572314e902e430ddc21dead3de6d08462b702d51a525
MD5 78ffb719459d2d248b92db6937e32aca
BLAKE2b-256 6fc9839269bdd1489f0309c51d1c8f97e7f28c3c8d7c80c5cf3ec1071e7c72c7

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: django_nplus1-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 19.5 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c62d4f7526e16c2e7a88090fec29deee1cae372470fed52a9d33826a0b8be78a
MD5 8a1853598eefefdad4c9820947372999
BLAKE2b-256 539eb1d2a17839acff366affbcba812cb83c46ba09d0103215e016f9ba5754ff

See more details on using hashes here.

Provenance

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