django-zen-queries
raw JSON → 2.1.0 verified Mon Apr 27 auth: no python
A Django library for explicit control over query execution, preventing accidental database queries in production code. Current version 2.1.0, requires Python >=3.6 and Django >=2.0. Actively maintained with minor releases.
pip install django-zen-queries Common errors
error AssertionError: Database queries disabled inside a queries_disabled context ↓
cause A database query was executed inside a `queries_disabled()` context.
fix
Identify the query and either move it outside the context or use
fetch() to explicitly allow a prefetch or specific query. error AttributeError: module 'zen_queries' has no attribute 'queries_disabled' ↓
cause Using an older version (pre-1.0?) or incorrect import; the API was renamed.
fix
Ensure you have installed django-zen-queries 1.0+ and use
zen_queries.queries_disabled(). Warnings
breaking Version 2.0.0 changed the internal implementation from monkeypatching to Django's Database Instrumentation API. Requires Django >=2.0. Django <2.0 is no longer supported. ↓
fix Upgrade to Django 2.0+ and django-zen-queries 2.0.0+.
gotcha Template permission checks (e.g., {% if perms %}) may trigger database queries even inside a queries_disabled block, because they are evaluated during template rendering. This can cause unexpected errors. ↓
fix Use zen_queries.TemplateResponse instead of Django's TemplateResponse in class-based views, or evaluate permissions outside templates.
gotcha The context manager does not automatically disable queries in template rendering. Use zen_queries.TemplateResponse or the {% queries_disabled %} template tag (added in 2.1.0) to cover templates. ↓
fix Wrap template rendering with {% queries_disabled %} in templates or use zen_queries.TemplateResponse.
Imports
- zen_queries wrong
from zen_queries import ...correctimport zen_queries - fetch wrong
from django.db import connections; ...correctfrom zen_queries import fetch
Quickstart
import zen_queries
from django.db import connection
with zen_queries.queries_disabled():
# Any query executed here will raise an error
# pass
# Or use as a decorator on a function
@zen_queries.queries_disabled()
def my_view(request):
# Queries disabled inside
pass