A drop-in Django model field for generating short, URL-safe, and unique IDs using shortuuid.
Customizable, migration-friendly, with optional collision detection and Django admin integration.
- Short, URL-safe unique IDs for Django models (default 22 characters, configurable)
- Customizable alphabet, prefix, and length
- Optional database collision detection and retry logic
- Django admin: field is read-only but visible
- Compatible with Django migrations
- Python type hints
pip install django-shortuuid
from django.db import models
from django_shortuuid.fields import ShortUUIDField
class MyModel(models.Model):
id = ShortUUIDField(primary_key=True, prefix="id-")
# ... other fields ...By default, auto=True generates a unique short UUID when each instance is saved, and makes the field read-only in forms/admin.
See django_example/ for a full Django setup and admin usage.
auto: Generate and set value automatically (Trueby default)length: Length of the generated unique string (default22)prefix: Optional string prefix for the field valuealphabet: Custom alphabet for uuid generation (default:shortuuid’s default)collision_check: Enables collision checking in the database (Trueby default)max_retries: Max attempts to generate unique value before error (10by default)
This project was inspired by benrobster/django-shortuuidfield
and builds on ideas from the original implementation.
Special thanks to shortuuid for the short, URL-safe UUID generation.