Skip to main content

Stripe-like prefixed IDs for Django. Works with existing UUIDs — no schema changes.

Project description

django-display-ids

PyPI Python Django CI codecov Docs License: MIT

Stripe-like prefixed IDs for Django. Works with existing UUIDs — no schema changes.

Why?

UUIDv7 (native in Python 3.14+) offers excellent database performance with time-ordered indexing. But they lack context — seeing 550e8400-e29b-41d4-a716-446655440000 in a URL or log doesn't tell you what kind of object it refers to.

Display IDs like inv_2aUyqjCzEIiEcYMKj7TZtw are more useful: the prefix identifies the object type at a glance, and they're compact and URL-safe. But storing display IDs in the database is far less efficient than native UUIDs.

Different consumers have different needs:

  • Humans prefer slugs (my-invoice) or display IDs (inv_xxx)
  • APIs and integrations work well with UUIDs

This library gives you the best of both worlds: accept any format in your URLs and API endpoints, then translate to an efficient UUID lookup in the database. Store UUIDs, expose whatever format your users need.

Installation

pip install django-display-ids

Add to INSTALLED_APPS:

INSTALLED_APPS = [
    # ...
    "django_display_ids",
]

Quick Start

Add the mixin to your model:

# models.py

import uuid
from django.db import models
from django_display_ids import DisplayIDModel

class Invoice(DisplayIDModel, models.Model):
    display_id_prefix = "inv"
    uuid_field = "uuid"
    uuid = models.UUIDField(default=uuid.uuid7, unique=True)
    slug = models.SlugField(unique=True)

Update your URLconf:

# urls.py

from django.urls import path, register_converter
from django_display_ids import DisplayIDOrUUIDOrSlugConverter

register_converter(DisplayIDOrUUIDOrSlugConverter, "identifier")

urlpatterns = [
    path("invoices/<identifier:id>/", InvoiceDetailView.as_view()),
]

Add the mixin to your view:

# views.py

# Django CBV
from django.views.generic import DetailView
from django_display_ids import DisplayIDMixin

class InvoiceDetailView(DisplayIDMixin, DetailView):
    model = Invoice
    lookup_param = "id"

# Django REST Framework
from rest_framework.viewsets import ModelViewSet
from django_display_ids.contrib.rest_framework import DisplayIDMixin

class InvoiceViewSet(DisplayIDMixin, ModelViewSet):
    queryset = Invoice.objects.all()
    serializer_class = InvoiceSerializer
    lookup_url_kwarg = "id"

Your views now accept:

  • inv_2aUyqjCzEIiEcYMKj7TZtw (display ID)
  • 550e8400-e29b-41d4-a716-446655440000 (UUID)
  • my-invoice (slug)

Update your templates:

{% load display_ids %}

{{ invoice.display_id }}                       {# inv_2aUyqjCzEIiEcYMKj7TZtw #}
{{ order.customer_id|display_id:"cust" }}      {# encode any UUID #}

Features

  • Multiple identifier formats: display ID (prefix_base62uuid), UUID (v4/v7), slug
  • Framework support: Django CBVs and Django REST Framework
  • Template filter: Encode UUIDs as display IDs in templates
  • Zero model changes required: Works with any existing UUID field
  • OpenAPI integration: Automatic schema generation with drf-spectacular

Documentation

Full documentation at django-display-ids.readthedocs.io.

Contributing

See CONTRIBUTING.md.

Related Projects

If you need ID generation and storage (custom model fields), consider:

django-display-ids works with existing UUID fields and handles resolution only — no migrations required.

Project details


Download files

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

Source Distribution

django_display_ids-0.7.0.tar.gz (20.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_display_ids-0.7.0-py3-none-any.whl (31.0 kB view details)

Uploaded Python 3

File details

Details for the file django_display_ids-0.7.0.tar.gz.

File metadata

  • Download URL: django_display_ids-0.7.0.tar.gz
  • Upload date:
  • Size: 20.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for django_display_ids-0.7.0.tar.gz
Algorithm Hash digest
SHA256 7ce0936aa3edac256514ca1c7d3b23b804fd498c3ee06f4a78a5d417f7ee25f3
MD5 bbb4d3a3136d7b3dac0eb4471fc486fc
BLAKE2b-256 fba11dfc6e6521b85a891fa15fac02947c109e83a739c4865f4b9f75b2eff737

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_display_ids-0.7.0.tar.gz:

Publisher: ci.yml on josephabrahams/django-display-ids

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_display_ids-0.7.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_display_ids-0.7.0-py3-none-any.whl
Algorithm Hash digest
SHA256 264498b7946efaace718cccba7ac5d5a65a1c786b19878a8bda3de8f17fdafd5
MD5 91f6ed6396ef9872d6f37b138d4a0cef
BLAKE2b-256 30d489a7eb4ab8ce2f26ad172003b2bd0218fd61a4ec9fd9f539b13bd14a39fb

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_display_ids-0.7.0-py3-none-any.whl:

Publisher: ci.yml on josephabrahams/django-display-ids

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

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page