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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
File details
Details for the file django-randomcharfield-0.0.1.tar.gz
.
File metadata
- Download URL: django-randomcharfield-0.0.1.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
Algorithm | Hash digest | |
---|---|---|
SHA256 | 28ec00b7f6c348d44f58c57a2c73183c6a5f888a005ffd5bdd2183ce3a389708 |
|
MD5 | 84477e641aec64fee3ae13a529b0685f |
|
BLAKE2b-256 | b6a63ab7d05fa272d89f92f8bf44074f9ade336ac81fdaf48cf2ea5b8fb9c513 |
File details
Details for the file django_randomcharfield-0.0.1-py3.8.egg
.
File metadata
- Download URL: django_randomcharfield-0.0.1-py3.8.egg
- Upload date:
- Size: 4.0 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
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5ca935595530f75588b46a6e9fc5e1f0ef78bea0fed2a9bb28f489669ebe79c6 |
|
MD5 | 76089d820f2b56d75674d03a9adc5641 |
|
BLAKE2b-256 | 18eeee553327a8bc4cfce1a2db83bac8e04108578b2f7098f07cd20e4908f45d |
File details
Details for the file django_randomcharfield-0.0.1-py3-none-any.whl
.
File metadata
- Download URL: django_randomcharfield-0.0.1-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
Algorithm | Hash digest | |
---|---|---|
SHA256 | b18707664adb33a0f4ffd0c920ed016351cdf2db57028102d3da759555979bc4 |
|
MD5 | c30d12e1f792b8bd908ab8be80cd5e7e |
|
BLAKE2b-256 | ec3089820ba71b6fb5510f76e977d6a603f2bc63d01986b1943cf60290c283fa |