django-zen-queries
Explicit control over query execution in Django applications.
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-queriesuv
uv add django-zen-queriespoetry
poetry add django-zen-queriesInstalling 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
More Quality Assurance packages
Coverage.py measures which lines of Python code…
permissive · top 1,000 on PyPI
ruffRuff is a Python linter and code formatter…
permissive · top 1,000 on PyPI
pexpectPexpect spawns and controls interactive console…
permissive · top 1,000 on PyPI
blackBlack reformats Python source code to a…
permissive · top 1,000 on PyPI
pytest-xdistpytest-xdist distributes pytest tests across…
permissive · top 1,000 on PyPI
cfn-lintValidates AWS CloudFormation templates in YAML…
permissive · top 1,000 on PyPI
django-pgactivityProvides Django management commands and…
permissive · top 5,000 on PyPI
drf-access-policyDeclares access control rules for Django REST…
permissive · top 15,000 on PyPI
djangorestframework-role-filtersAdds role-based access control to Django REST…
permissive · top 5,000 on PyPI
drf-dynamic-fieldsProvides a Django REST Framework mixin that…
permissive · top 15,000 on PyPI
django-mock-queriesMocks Django QuerySet operations in memory for…
permissive · top 15,000 on PyPI
django-query-builderBuilds and executes complex SQL queries…
permissive · top 15,000 on PyPI
django-ordered-modelAdds ordering and reordering capabilities to…
permissive · top 15,000 on PyPI
adrfAdds async/await support to Django REST…
permissive · top 15,000 on PyPI
django-pg-returningExtends Django's ORM to capture and return rows…
permissive · top 15,000 on PyPI
django-read-onlyPrevents Django database write operations by…
permissive · top 15,000 on PyPI