Skip to main content

Django Utils

CI PyPI version Python versions Documentation

The data__filling dropdown filter open in the Django admin sidebar, listing Avocado, Bacon, Cheddar, Corned Beef and Turkey

Django Utils is a collection of small Django helpers, admin power-ups and ORM utilities that make common patterns shorter, safer, and ASGI-friendly — JSON-aware admin filters and a validating JSON widget, collision-free slug mixins and metadata-carrying choices, encrypted model fields, a native PostgreSQL enum field, memory-bounded queryset iteration and fan-out-safe subquery aggregates, chunked management commands, contextvars-based request context, and header-based CSRF hardening. It builds on the Python Utils library, is by no means a complete collection, but 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

  1. Run pip install django-utils2
  2. Add django_utils to your INSTALLED_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 — no custom SimpleListFilter needed:

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. See the quickstart page for two more 2-minute wins.

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 JSONField admin widget that pretty-prints and validates JSON as you type. See JSON widget.
  • Read-only admin — turn any ModelAdmin into 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.Enum on demand via as_enum(). See Choices.
  • PostgreSQL ENUM field — a CharField backed by a native PostgreSQL ENUM type on Postgres, plain VARCHAR everywhere 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 helperssuperuser_required/staff_required decorators and a permission_string() builder for has_perm() checks. See Auth helpers.
  • Fetch-Metadata CSRF middleware — defense-in-depth CSRF hardening using the Sec-Fetch-Site header, run alongside (not instead of) Django's own CSRF middleware. See Fetch-Metadata CSRF middleware.
  • Subquery aggregatesSubqueryCount/SubquerySum/SubqueryAvg/... annotate each aggregate in its own subquery, avoiding the JOIN fan-out that multiplies counts. See Subquery aggregates.
  • Bulk upsertbulk_update_or_create(): one INSERT ... ON CONFLICT DO UPDATE batch instead of a racy, two-query-per-row loop. See Bulk upsert.
  • Chunked management commandsChunkedCommand: memory-bounded, resumable processing of large querysets with progress logging and a transactional dry-run. See ChunkedCommand.
  • Encrypted model fieldsEncryptedCharField/EncryptedTextField/ EncryptedJSONField: Fernet-encrypted values in a plain TEXT column, with key-rotation support. See Encrypted model fields.

Links

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_utils2-4.1.0.tar.gz (58.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_utils2-4.1.0-py3-none-any.whl (75.1 kB view details)

Uploaded Python 3

File details

Details for the file django_utils2-4.1.0.tar.gz.

File metadata

  • Download URL: django_utils2-4.1.0.tar.gz
  • Upload date:
  • Size: 58.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for django_utils2-4.1.0.tar.gz
Algorithm Hash digest
SHA256 3c8cb68895c87dd282fd208c1759cb8fc279b3b706f45e65f19363c6c153a972
MD5 6b65716d1306b7d75ee980743b78ffe1
BLAKE2b-256 146c3db918ab49aa40ce34a1e0d42a0094a47ae0c4c6ccba8958c2b1a2c1e996

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_utils2-4.1.0.tar.gz:

Publisher: publish.yml on wolph/django-utils

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_utils2-4.1.0-py3-none-any.whl.

File metadata

  • Download URL: django_utils2-4.1.0-py3-none-any.whl
  • Upload date:
  • Size: 75.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for django_utils2-4.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ad9f9202425afbc312a3f26c141bf3a9b1007eaf27a28a578b687c2600e05c47
MD5 7834a211173d5671f9c57e16d8d5ca63
BLAKE2b-256 ab2d138352fb1fddad30847f80486ede617ed94d4275ba44dcf5421ab9c29d85

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_utils2-4.1.0-py3-none-any.whl:

Publisher: publish.yml on wolph/django-utils

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

4.1.2

2 files

4.1.1

2 files

This release

4.1.0 This release

2 files

3.0.2

2 files

3.0.1

2 files

3.0.0

2 files

2.12.1

2 files

2.12.0

2 files

2.11.3

2 files

2.11.2

2 files

2.11.1

2 files

2.11.0

2 files

2.9.0

2 files

2.8.0

2 files

2.7.0

2 files

2.6.0

2 files

2.5.4

2 files

2.5.3

2 files

2.5.2

2 files

2.5.1

2 files

2.5.0

2 files

2.4.0

2 files

2.3.0

2 files

2.2.1

2 files

2.2.0

2 files

2.1.0

1 file

2.0.1

1 file

2.0.0

1 file

1.8.1

1 file

1.8.0

1 file

1.7.1

1 file

1.7.0

1 file

1.6.1

1 file

1.6.0

1 file

1.5.1

1 file

1.1

1 file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page