Skip to main content

Overview

django-prefix-id is a simple package that contains a single Django model field, PrefixIDField. PrefixIDField is

  • based on Django's CharField
  • allows using a prefix (recommended for context)
    • enables some very interesting product usage and querying patterns
  • autogenerates a unique id by default (auto-including the prefix, if provided)
    • uses uuid.uuid4() to generate a 128-bit integer, which is then base62-encoded
  • auto-sets the max_length field property, factoring in the provided prefix length and a known max length of 22 characters for a base62-encoded 128-bit integer

Why work with IDs like 7584358 or '7fca5f1d-f867-4696-960b-e57274cc5647' when you could have something swanky like 'user_4wr7jBDTEqVBCsXEih4zfP'?

If you already like to use UUIDs as IDs, you should use django-prefix-id! If you currently use autoincrementing IDs, you should also consider using django-prefix-id, from an obfuscation point of view.

Installation

$ pip install django-prefix-id

Usage

from prefix_id import PrefixIDField

class Book(models.Model):
    id = PrefixIDField(prefix="book", primary_key=True)
    title = models.CharField(max_length=100, blank=False)
    ...

Make your migrations, as you normally would:

$ python manage.py makemigrations

Development

$ pip -m venv venv
$ source /venv/bin/activate
$ pip install -r requirements.txt

Testing

Manually run tests:

$ pytest

Test coverage is at 100%. To get test coverage output:

$ pytest --cov --cov-report html
# OR
$ pytest --cov

To cover a breadth of likely usage scenarios, tox is the tool of choice. It is set up with a matrix covering various combinations of Python versions 3.8 - 3.12 with Django 4.2 and 5.0.

See tox.ini to enable testing Django versions as far back as Django 3.2 - tests pass across the board, but for simplicity only latest LTS Django and other currently supported versions will be considered in scope.

Run the test suite against all enabled versions:

$ tox

Backstory and Inspiration

Stripe-like prefixed entity IDs have long been a topic of interest to me, and the improved user experience that they can provide. I'd gone through a couple of iterations with FastAPI and Django - and thought I might as well release something that I'm happy with sharing publicly.

About 80% through this effort, I found yunojuno's django-charid-field project, and took some further inspiration from it - so thanks to them!

(It also appears we have other project interests in common!)

Metadata

Release files for django-prefix-id 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-prefix-id 1.0.1
File Size Uploaded Provenance
django_prefix_id-1.0.1.tar.gz 5.3 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for django-prefix-id 1.0.1
File Interpreter ABI Platform
django_prefix_id-1.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 11.1 kB

Release files / django_prefix_id-1.0.1.tar.gz

Download URL django_prefix_id-1.0.1.tar.gz
Size 5.3 kB
Tags Source
SHA-256 checksum
How to use checksums
5d4f849851e6343436af0b754afd9b620223eaab062eca5aa7b615ddc6c8ac74
BLAKE2b-256 checksum
How to use checksums
7cc928219867c52600f6992db8dd959588f303d51dbeee15e59c21ea7d02cc1b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.2

Release files / django_prefix_id-1.0.1-py3-none-any.whl

Download URL django_prefix_id-1.0.1-py3-none-any.whl
Size 5.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a8fc4095912fea331b12f1199d56d726ae07e5c27c67d65969b0b3e485fa9882
BLAKE2b-256 checksum
How to use checksums
5d36415ab037f4da8b1c81240392d76196b940e9ae3eb1f3492d5525869ddf29
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.2

Security for django-prefix-id 1.0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page