skillfed

django-zen-queries

Explicit control over query execution in Django applications.

django-zen-queries v2.1.0 155.8K downloads/30d#10,799 on PyPI388
Permissive license BSD Active released

What it is and what it does

django-zen-queries is a Django debugging and design tool that makes database query execution explicit rather than implicit. It provides context managers and decorators that prevent code from running queries unless explicitly allowed, forcing developers to think about when and where data is fetched versus rendered. The package addresses a fundamental Django ORM weakness: lazy evaluation hides query execution, making it easy to accidentally trigger the N+1 problem when templates or serializers access related data without proper prefetching.

The package includes a queries_disabled context manager and decorator, a fetch utility to force queryset evaluation, and integration helpers for Django templates and Django REST Framework serializers. It does not optimize queries itself—instead, it fails loudly when queries run where they shouldn't, pushing developers to design code that separates data fetching from data rendering.

Use it for:

  • Wrap template rendering in views to catch N+1 query problems before production.
  • Decorate serializer methods to prevent REST Framework endpoints from unexpectedly querying during serialization.
  • Enforce architectural boundaries where certain functions should never trigger database access.
  • Debug performance regressions by identifying which code paths execute unexpected queries.
  • Use as a development-time safety net to catch query problems introduced by template or serializer changes.

Worth the install?

AI-flagged interpretation of the facts on this page — verify before relying

Prevents unintended database queries in Django views and serializers by enforcing explicit query execution zones through context managers and decorators.

Yes, if you develop Django applications and want to catch N+1 and other implicit query problems during development. The package is lightweight, actively maintained, and widely used. Install it in development environments to enforce explicit query boundaries; the overhead is negligible for a debugging tool. Most teams use it as a development-time safety net rather than in production.

Install

django-zen-queries on PyPI

pip

pip install django-zen-queries

uv

uv add django-zen-queries

poetry

poetry add django-zen-queries

Installing django-zen-queries

Before you install

Low friction install with a single runtime dependency on Django. Actively maintained with recent commits and steady adoption.

License in practice

BSD license is permissive and poses no restrictions on commercial or private use.

Quickstart

pip install django-zen-queries

from django_zen_queries import queries_disabled, fetch

def menu(request):
    pizzas = fetch(Pizza.objects.all())
    with queries_disabled():
        return render(request, 'menu.html', {'pizzas': pizzas})

Requires Django and Python 3.6 or later.

Verify before relying

  • Whether the package works with Django versions released after 3.1 (last tested version in description).
  • Performance overhead of the query-blocking mechanism in production environments.
  • Current compatibility with modern Django and Python versions beyond those mentioned in the description.

Package facts

License BSD (permissive)
Python support supports the current Python release (>=3.6)
Install friction low — pure-Python wheel
Runtime dependencies 1 — Django
Maintenance actively maintained — 1,844 days since the last release
Last repo commit
First released
Downloads 155,817/month — #10,799 on PyPI (30-day window, as of 2026-08-14)
Known vulnerabilities none known (OSV.dev, checked 2026-08-14)

Evidence: django_zen_queries-2.1.0-py3-none-any.whl

Tags

django query controlprevent implicit database queriesdjango n+1 problem detectionexplicit query execution djangodjango performance debuggingquery execution context managerdjango lazy evaluation control
django-performancequery-debuggingorm-control

More Quality Assurance packages

coverage

Coverage.py measures which lines of Python code…

permissive · top 1,000 on PyPI

ruff

Ruff is a Python linter and code formatter…

permissive · top 1,000 on PyPI

pexpect

Pexpect spawns and controls interactive console…

permissive · top 1,000 on PyPI

black

Black reformats Python source code to a…

permissive · top 1,000 on PyPI

pytest-xdist

pytest-xdist distributes pytest tests across…

permissive · top 1,000 on PyPI

cfn-lint

Validates AWS CloudFormation templates in YAML…

permissive · top 1,000 on PyPI

django-pgactivity

Provides Django management commands and…

permissive · top 5,000 on PyPI

drf-access-policy

Declares access control rules for Django REST…

permissive · top 15,000 on PyPI

djangorestframework-role-filters

Adds role-based access control to Django REST…

permissive · top 5,000 on PyPI

drf-dynamic-fields

Provides a Django REST Framework mixin that…

permissive · top 15,000 on PyPI

django-mock-queries

Mocks Django QuerySet operations in memory for…

permissive · top 15,000 on PyPI

django-query-builder

Builds and executes complex SQL queries…

permissive · top 15,000 on PyPI

django-ordered-model

Adds ordering and reordering capabilities to…

permissive · top 15,000 on PyPI

adrf

Adds async/await support to Django REST…

permissive · top 15,000 on PyPI

django-pg-returning

Extends Django's ORM to capture and return rows…

permissive · top 15,000 on PyPI

django-read-only

Prevents Django database write operations by…

permissive · top 15,000 on PyPI