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
Release files for django-randomcharfield 0.0.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| django-randomcharfield-0.0.3.tar.gz | 2.7 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| django_randomcharfield-0.0.3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 6.5 kB
Release files / django-randomcharfield-0.0.3.tar.gz
| Download URL | django-randomcharfield-0.0.3.tar.gz |
|---|---|
| Size | 2.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ef525371a496279eaad2b3b5ae8528523d129edd36234818392fa059011f4051
|
|
BLAKE2b-256 checksum How to use checksums |
c07c03214782edc48b01adaa38baef08f92457dc89a33294959c86132f6a5c4b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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
|
Release files / django_randomcharfield-0.0.3-py3-none-any.whl
| Download URL | django_randomcharfield-0.0.3-py3-none-any.whl |
|---|---|
| Size | 3.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
36d193b6568c15a95bd5ed95007fe7bca6551295f32c81fbf05fbcd82f989d7b
|
|
BLAKE2b-256 checksum How to use checksums |
1c7c5c3a4e044ba8742cee10612bf55d08a4d14ab5b7a29572b5e019e384ff38
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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
|