Skip to main content

A simple prefix ID Django model field, using UUID to Base62 encoding.

Project description

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!)

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-prefix-id-1.0.0.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

django_prefix_id-1.0.0-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file django-prefix-id-1.0.0.tar.gz.

File metadata

  • Download URL: django-prefix-id-1.0.0.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for django-prefix-id-1.0.0.tar.gz
Algorithm Hash digest
SHA256 ecb2776f63ca7d1f4ce2bdceae3962244fd3801eb28a644acbaeafa520f77468
MD5 a24bf5d4f09fca907ed4ea9c803937cd
BLAKE2b-256 f2a062fdf16122109081cb8e6662ca859ba99f84e05d4734fe07a7a24cc06a7f

See more details on using hashes here.

File details

Details for the file django_prefix_id-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_prefix_id-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8473d407ba7c9a7c4f98ac8fdd802b5a4d0927a296774052bd04a59455e0451b
MD5 e3c004d5fe86a187604da86f4f50dd54
BLAKE2b-256 b3cbe91df36edcb8ab779e2d10decac6c3b3f858f4312a1a18d9f7f08b147b24

See more details on using hashes here.

Supported by

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