Django Utils
Django Utils is a collection of the small Django helpers, admin power-ups and ORM utilities that most projects end up writing themselves: JSON-aware admin filters, encrypted model fields, memory-bounded queryset iteration, resumable management commands, and more. It builds on the Python Utils library. It is by no means a complete collection, but it has served production projects well and keeps growing.
Full documentation, including a "why this over the alternatives" page, is at https://django-utils-2.readthedocs.io/en/latest/.
Requirements
- Python 3.10+
- Django 4.2, 5.2, or 6.0
Install
- Run
pip install django-utils2 - Add
django_utilsto yourINSTALLED_APPS
If you want to run the tests, install the tests extra
(pip install "django-utils2[tests]") and run pytest.
Quickstart
Filter an admin changelist on a nested JSONField value without
writing a custom SimpleListFilter:
from django.contrib import admin
from django_utils.admin.filters import JSONFieldFilterDropdown
from myapp.models import Sandwich
class SandwichAdmin(admin.ModelAdmin):
list_filter = (
JSONFieldFilterDropdown.create('data__filling'),
)
admin.site.register(Sandwich, SandwichAdmin)
A sidebar filter on data['filling'], no JOIN or denormalized column.
The quickstart
page has two more features like this.
Features
Every feature below has its own page on the docs site, with runnable examples, edge cases, and (for the admin features) screenshots or a live in-browser demo.
- Admin dropdown / Select2 / JSON filters: dropdown, autocomplete, and
JSON sub-path list filters for the changelist sidebar, plus an operator
selector (
gte,lte,icontains, ...) for typed comparisons. See Select / dropdown / autocomplete filters and Operator filters. - JSON widget: a drop-in
JSONFieldadmin widget that pretty-prints and validates JSON as you type. See JSON widget. - Read-only admin: turn any
ModelAdmininto a read-only view: add/change/delete denied for everyone, filtering and search still work. See Read-only admin. - Count columns: sortable related-object count columns for
list_display, without the N+1 or JOIN fan-out footguns. See Count columns. - Admin export: streaming CSV/JSON export actions for the changelist,
memory-bounded via
queryset_iterator. See Export. - Choices: metadata-carrying choices with dict-like access and a real
enum.Enumon demand viaas_enum(). See Choices. - PostgreSQL ENUM field: a
CharFieldbacked by a native PostgreSQLENUMtype on Postgres, plainVARCHAReverywhere else, with hand-written migration operations for creating the type and adding values. See PostgreSQL ENUM field. - Request/user context (ASGI-safe): access the current request/user
from anywhere via
contextvars, isolated per asyncio task, unlike thread-local equivalents. See Current request / user (ASGI-safe). - Query budgets: catch N+1 regressions in production code paths, not just in tests, with a query-counting context manager/decorator. See Query budgets.
- Auth helpers:
superuser_required/staff_requireddecorators and apermission_string()builder forhas_perm()checks. See Auth helpers. - Fetch-Metadata CSRF middleware: defense-in-depth CSRF hardening
using the
Sec-Fetch-Siteheader, run alongside (not instead of) Django's own CSRF middleware. See Fetch-Metadata CSRF middleware. - Subquery aggregates:
SubqueryCount/SubquerySum/SubqueryAvg/... annotate each aggregate in its own subquery, avoiding the JOIN fan-out that multiplies counts. See Subquery aggregates. - Bulk upsert:
bulk_update_or_create(): oneINSERT ... ON CONFLICT DO UPDATEbatch instead of a racy, two-query-per-row loop. See Bulk upsert. - Chunked management commands:
ChunkedCommand: memory-bounded, resumable processing of large querysets with progress logging and a transactional dry-run. See ChunkedCommand. - Encrypted model fields:
EncryptedCharField/EncryptedTextField/EncryptedJSONField: Fernet-encrypted values in a plainTEXTcolumn, with key-rotation support. See Encrypted model fields.
Links
- Documentation: https://django-utils-2.readthedocs.io/en/latest/
- Source: https://github.com/WoLpH/django-utils
- Bug reports: https://github.com/WoLpH/django-utils/issues
- Package homepage: https://pypi.org/project/django-utils2/
- My blog: http://w.wol.ph/
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_utils2-4.1.2.tar.gz.
File metadata
- Download URL: django_utils2-4.1.2.tar.gz
- Upload date:
- Size: 61.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cd27162027d22f6ee0bb1b87d162de2e0f6dc3c2c6a40e08118fd25b5c6e4269
|
|
| MD5 |
05097b7d43d2cd2b136202aab9317af3
|
|
| BLAKE2b-256 |
49485331a499a666d84cfa53686aba19ad33a0a019ba720fde40edf03170801d
|
Provenance
The following attestation bundles were made for django_utils2-4.1.2.tar.gz:
Publisher:
publish.yml on wolph/django-utils
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_utils2-4.1.2.tar.gz -
Subject digest:
cd27162027d22f6ee0bb1b87d162de2e0f6dc3c2c6a40e08118fd25b5c6e4269 - Sigstore transparency entry: 2406576311
- Sigstore integration time:
-
Permalink:
wolph/django-utils@a68fb1776142049175f9f36934b68d8b1d5b19e3 -
Branch / Tag:
refs/tags/v4.1.2 - Owner: https://github.com/wolph
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@a68fb1776142049175f9f36934b68d8b1d5b19e3 -
Trigger Event:
push
-
Statement type:
File details
Details for the file django_utils2-4.1.2-py3-none-any.whl.
File metadata
- Download URL: django_utils2-4.1.2-py3-none-any.whl
- Upload date:
- Size: 77.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
acdeb39af8ea769770080ed4d4bf136729a8f4437ca8bb9e36799eacdc6dd976
|
|
| MD5 |
be7f044c3a48100110b30727137f90a6
|
|
| BLAKE2b-256 |
fdeb9ab017cdfc35a80f61494b66d86aefc17cbb04b9aa148a2d315ef66206a6
|
Provenance
The following attestation bundles were made for django_utils2-4.1.2-py3-none-any.whl:
Publisher:
publish.yml on wolph/django-utils
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_utils2-4.1.2-py3-none-any.whl -
Subject digest:
acdeb39af8ea769770080ed4d4bf136729a8f4437ca8bb9e36799eacdc6dd976 - Sigstore transparency entry: 2406576341
- Sigstore integration time:
-
Permalink:
wolph/django-utils@a68fb1776142049175f9f36934b68d8b1d5b19e3 -
Branch / Tag:
refs/tags/v4.1.2 - Owner: https://github.com/wolph
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@a68fb1776142049175f9f36934b68d8b1d5b19e3 -
Trigger Event:
push
-
Statement type: