/python-ulid-django

ULID (Universally Unique Lexicographically Sortable Identifier) support for Django.

Primary LanguagePythonMIT LicenseMIT

python-ulid-django

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 use python-ulib's ULID implementation.

Usage

Installation

pip install python-ulid-django

Model Field

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)

URL Converter

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),
    ...,
]

Development

Prerequisite: PDM

Environment setup: pdm sync

Run linters: pdm lint

Test: pdm test