prefixed-ulids
Prefixed ULID Primary Keys
This project uses Stripe-style prefixed ULIDs as the primary key for every Django model:
prd_01jq3v8f6a7b2c8d9e0f1g2h3j4k
ord_01jq3v8fgh7x2y5z9a1b2c3d4e5f
A 3-4 character prefix identifies the entity type, followed by an underscore and a lowercase ULID. ULIDs are 128-bit, lexicographically sortable by creation time, URL-safe, and collision-resistant.
Why
- Debuggable.
ord_01jq...in a log line tells you immediately it's an order — no need to cross-reference the column. - Safe to expose. Unlike auto-increment integers, prefixed ULIDs leak no ordering or volume information, and unlike opaque UUIDs they remain human-readable.
- Time-sortable. ULIDs sort chronologically, so
ORDER BY iddoubles asORDER BY created_atwithout a second index. - Type-safe across layers. Every ID is a
strend-to-end — noUUID/strcoercion at the service/API boundary. - No integer collisions. Exporting, importing, and sharding are all easier without monotonic counters.
The Generator
Put this in src/project/ids.py:
from ulid import ULID
def prefixed_ulid(prefix: str) -> str:
return f"{prefix}_{str(ULID()).lower()}"
def _make_generator(prefix: str):
def generate() -> str:
return prefixed_ulid(prefix)
generate.__name__ = f"generate_{prefix}_id"
generate.__qualname__ = f"generate_{prefix}_id"
return generate
Then register a generator per aggregate root:
generate_prd_id = _make_generator("prd")
generate_ord_id = _make_generator("ord")
generate_itm_id = _make_generator("itm")
The __name__ / __qualname__ rewrite matters: Django migrations serialize the default callable's fully qualified name, so each generator needs a distinct identity or the autodetector will get confused.
Choosing a Prefix
- 3 to 4 lowercase letters — short enough to stay readable in logs
- Must be unique across the whole project
- Prefer mnemonic, not cryptic:
ordfor order,invfor invoice,prdfor product,usrfor user - Avoid collisions with existing prefixes — grep
src/project/ids.pybefore inventing a new one - Never rename a prefix once it's in production; the prefix is part of the ID
Using it in a Model
from typing import ClassVar
from django.db import models
from project.ids import generate_prd_id
class Product(models.Model):
__prefix__: ClassVar[str] = "prd"
id = models.CharField(
max_length=64,
primary_key=True,
default=generate_prd_id,
editable=False,
)
name = models.CharField(max_length=255)
def __str__(self) -> str:
return self.name
Rules:
CharField(max_length=64)— ULID is 26 chars, prefix + separator adds up to ~10, 64 leaves headroom.primary_key=Trueandeditable=False.- The
defaultis the generator function (no parentheses) so Django calls it per row. __prefix__: ClassVar[str]mirrors the generator's prefix — makes the mapping discoverable from the model class alone, and lets tests assert on it.- Never override
save()to generate the ID; the default handles it.
Using it Across the Stack
Once IDs are strings at the ORM layer, they stay strings everywhere else:
- DTOs (Pydantic):
id: str— neverUUID. - Repository params:
def get(self, product_id: str) -> ProductDTO: ... - API path params (django-ninja):
def get_product(request, product_id: str): ... - Celery task args: pass the string ID, never a model instance.
- Tests: assert on the prefix, e.g.
assert dto.id.startswith("prd_").
The prefix is also a cheap sanity check on every boundary: if an ID ever shows up without its prefix, something has stripped or regenerated it incorrectly.
Migrating an Existing Table
If the table already has integer or UUID primary keys, don't try to change them in place. Instead:
- Add a new
CharFieldcolumn with the prefixed ULID default, nullable at first. - Backfill with a data migration that assigns
prefixed_ulid("prd")to every existing row. - Add
unique=Trueand make it non-nullable in a follow-up migration. - Swap it to
primary_key=Trueonly after every foreign key has been migrated to reference the new column — this usually means a multi-release cutover.
Prefer doing this on a new table where possible; in-place primary-key swaps in production are a lot of work for limited benefit.
Verify
- Every model has
__prefix__and aCharFieldprimary key using agenerate_*_iddefault. - Every
generate_*_idinsrc/project/ids.pyhas a unique prefix. - No model uses
UUIDField,AutoField, orBigAutoFieldfor its primary key. - No DTO field, service argument, or API path param types an ID as
UUID— they're allstr.
uv run ruff check src
uv run pyrefly check src
uv run pytest
More from dvf/opinionated-django
services
Structure Django business logic as plain services that receive their dependencies via constructor injection, and wire them through an svcs registry so they can be resolved anywhere — views, Celery tasks, management commands, tests. Use when adding a new service, refactoring fat views or model methods into a service, wiring a service into the registry, or explaining where business logic should live in this project.
11architecture
Implement a Django feature following the opinionated architecture — prefixed ULID IDs, repository pattern, Pydantic DTOs, svcs service locator, project-scoped django-ninja API, Celery reliable signals, and layered tests. Use when the user asks to add a new entity, endpoint, app, or business logic in a Django project that follows these conventions.
11lint
Run linting, formatting, and static type checks on a Django project using ruff and pyrefly, and fix any issues found. Use after making code changes, before committing, or whenever the user asks to lint, format, or type-check the codebase.
10pytest
Set up and write pytest tests for an op-django project — pytest-django configuration, Celery eager mode for reliable-signal tests, freezegun for time-sensitive logic, shared conftest fixtures for DTOs and svcs overrides, and the three-layer test convention (repository against a real DB, service against mocked repos, API through HTTP). Use when adding tests to a new project, writing tests for a new feature, setting up test infrastructure, or explaining how tests should be organized.
10scaffold
Set up a Django project into the op-django layout so the architecture, signals, and settings skills have a foundation to build on. Use when starting a new project from scratch, or when converting an existing Django project to follow this opinionated structure. Creates the src/project/ shell (ids, services registry, api, reliable signals), installs dependencies with uv, and establishes the per-app directory conventions.
10signals
Add reliable signals (async side-effects via Celery) to a Django feature. Use when a business operation needs to trigger post-commit work like notifications, cache invalidation, analytics, or cross-service coordination — any time the user mentions side-effects, events, or async processing tied to a business action.
10