Django NanoID Field
NanoID is an alternative to UUID, CUID for generating random IDs. This package provides a field to use in models.
Roadmap
- Create
AutoFieldthat uses NanoID instead of regular integer ID.
Installation
Install it from PyPI:
pip install django-nanoid-field
Usage
You can use it in your models like:
from django.db import models
from nanoid_field import NanoidField
class Profile(models.Model):
hash = NanoidField(max_length=10, alphabet='0123456789abcdefghijklmnopqrstuvwxyz')
NanoidField
This model field is based on CharField. All of it's parameters can be used.
Additionally following fields affects outcome of NanoID:
max_length Parameter (Optional)
Determines size of the generated ID as well as length of the field in Database.
DEFAULT:
21
alphabet Parameter (Optional)
This optional parameter helps you to determine which characters will be used to build up.
DEFAULT:
0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz-
Using NanoidField as primary_key
To use NanoID as default primary_key you need to explicitly define it in your
model:
from django.db import models
class SomeModel(models.Model):
id = NanoidField()
# ...
A good practice is to create a base model. It also helps you to add generic methods to all of your models.
Create a file named base_model.py one of the root models directory:
from django.db import models
class BaseModel(models.Model):
id = NanoidField()
class Meta:
abstract = True
Then you can exten from it in any model you'd like:
from django.db import models
from app.models.base_model import BaseModel
class Product(BaseModel):
name = models.CharField()
Changing defaults
django-nanoid-field also provides two settings to change defaults of alphabet
and max_length. You can change those by adding them to your settings.py file
# settings.py file
NANOID_ALPHABET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz-"
NANOID_SIZE = 21
Metadata
Release files for django-nanoid-field 0.1.5
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_nanoid_field-0.1.5.tar.gz | 3.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| django_nanoid_field-0.1.5-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 7.6 kB
Release files / django_nanoid_field-0.1.5.tar.gz
| Download URL | django_nanoid_field-0.1.5.tar.gz |
|---|---|
| Size | 3.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
dd52887676d7fd1a0ec9e3510d8871235fec7cc28f0b3dcb5bd37ab2efa09fdd
|
|
BLAKE2b-256 checksum How to use checksums |
bfb91a85846bc22cb7b40406fdc4d587a65f6d135a646c92834d3e5573ace328
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.25
|
Release files / django_nanoid_field-0.1.5-py3-none-any.whl
| Download URL | django_nanoid_field-0.1.5-py3-none-any.whl |
|---|---|
| Size | 4.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c83269ae24d06e3099bedbc180fa8517b310e10727ba5a3d844d99daf2e5df2f
|
|
BLAKE2b-256 checksum How to use checksums |
f122c0f33a68360f180b08c2c350c33226c62a34a173bd2d5495bbe68886bdd7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.25
|