ULID (Universally Unique Lexicographically Sortable Identifier) support for Django.
This package uses the ULID type implemented by python-ulid
.
This package is heavily inspired by
django-ulid
. The reason I'm reinventing the wheel is that I want to usepython-ulib
's ULID implementation.
pip install python-ulid-django
You can then add ULIDField
to your Django model just like other fields.
Example:
from django.contrib.auth.models import AbstractUser
from ulid import ULID
from ulid_django.models import ULIDField
class User(AbstractUser):
id = ULIDField(primary_key=True, default=ULID, editable=False)
There is also a URL converter can be used.
from django.urls import path, register_converter
from ulid import ULID
from ulid_django.converters import ULIDConverter
def user_detail_view(request, user_id):
assert isinstance(user_id, ULID)
...
register_converter(ULIDConverter, "ulid")
urlpatterns = [
path("user/<ulid:user_id>/", user_detail_view),
...,
]
Prerequisite: PDM
Environment setup: pdm sync
Run linters: pdm lint
Test: pdm test