Skip to main content

Next generation NanoID in Django Model IDs

Project description

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 AutoField that 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

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_nanoid_field-0.1.5.tar.gz (3.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_nanoid_field-0.1.5-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

Details for the file django_nanoid_field-0.1.5.tar.gz.

File metadata

  • Download URL: django_nanoid_field-0.1.5.tar.gz
  • Upload date:
  • Size: 3.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.25

File hashes

Hashes for django_nanoid_field-0.1.5.tar.gz
Algorithm Hash digest
SHA256 dd52887676d7fd1a0ec9e3510d8871235fec7cc28f0b3dcb5bd37ab2efa09fdd
MD5 27cc380d04643567c342e61d17212133
BLAKE2b-256 bfb91a85846bc22cb7b40406fdc4d587a65f6d135a646c92834d3e5573ace328

See more details on using hashes here.

File details

Details for the file django_nanoid_field-0.1.5-py3-none-any.whl.

File metadata

File hashes

Hashes for django_nanoid_field-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 c83269ae24d06e3099bedbc180fa8517b310e10727ba5a3d844d99daf2e5df2f
MD5 5e0b980043ec4975958bcc8416590925
BLAKE2b-256 f122c0f33a68360f180b08c2c350c33226c62a34a173bd2d5495bbe68886bdd7

See more details on using hashes here.

Supported by

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