Skip to main content

An alternative to UUIDs for model fields

Project description

django-randomcharfield

What's this?

This is a custom Django model field written as an alternative to UUIDs.

It works by base64-encoding 20 bytes (160 bits) from urandom, as recommended by Neil Madden's fantastic blog post, "Moving away from UUIDs"

Installation

Use any package manager of choice. If using pip, install with this command:

pip install django-randomcharfield

Usage

Add this to your model definition as follows:

from django.db import models
from randomcharfield import (
    RandomCharField, generate_random_field,
)


class SomeModel(models.Model):
    uid = RandomCharField(default=generate_random_field)

Note that this doesn't make any assumptions about your current database state. That means you are responsible for creating the database index for this field, adding a uniqueness constraint, or making it a primary key.

If you have existing rows in your table and want to add a uniqueness constraint, you may want to set this field as nullable (i.e. passing in null=True) and to devise a plan to populate this. A good guide for such a plan is one written in the official Django documentation to deal with the same situation for UUIDField: https://docs.djangoproject.com/en/3.0/howto/writing-migrations/#migrations-that-add-unique-fields

Notes

This isn't a lot of code, so you should probably be just copying the field to your project if you want customisations, rather than proposing changes.

License

MIT

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-randomcharfield-0.0.2.tar.gz (2.7 kB view details)

Uploaded Source

Built Distribution

django_randomcharfield-0.0.2-py3-none-any.whl (3.8 kB view details)

Uploaded Python 3

File details

Details for the file django-randomcharfield-0.0.2.tar.gz.

File metadata

  • Download URL: django-randomcharfield-0.0.2.tar.gz
  • Upload date:
  • Size: 2.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.3.1 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.0

File hashes

Hashes for django-randomcharfield-0.0.2.tar.gz
Algorithm Hash digest
SHA256 379ba971558f8bb3ba1de9e802958ada45c38b04c2a43deaf27e9ef002827089
MD5 d53d183a583f6beef3bcf412a4fe1cb6
BLAKE2b-256 996653d4280cd8ff151b0a0e8630302f2303532cb79c862ace32c7a7f033a7fd

See more details on using hashes here.

File details

Details for the file django_randomcharfield-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: django_randomcharfield-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 3.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.3.1 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.0

File hashes

Hashes for django_randomcharfield-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 892f980d3dbbcc19b6502aa8ead59c735190187f59a2b9ed150ace5832b6de0d
MD5 d1761b9f72924017259b9868afe348c8
BLAKE2b-256 75d8fe82a7296f75358d0695abeb28ddf895530531fdf55141779eee404989ea

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