Skip to main content

A model base class which provides custom designed random integer primary keys to you Django models.

Project description

Django Random ID

A model base class which provides custom designed random integer primary keys to you Django models.

Installation

Run the following to install:

pip install django_random_id

Usage

The original way of creating and assigning primary keys in Django models

from django.db import models

class CustomUser(models.Model):
    name = models.CharField(max_length=30, blank=False)

steve = CustomUser.objects.create(name="Steve")
bill = CustomUser.objects.create(name="Bill")
print(steve.id) 
# >>> '1'
print(bill.id) 
# >>> '2'

The primary keys are auto-incremental integers.

Now, let's see how RandomIDModel helps you:

from django.db import models
from django_random_id import RandomIDModel

class CustomUser(RandomIDModel):
    name = models.CharField(max_length=30, blank=False)

steve = CustomUser.objects.create(name="Steve")
bill = CustomUser.objects.create(name="Bill")
print(steve.id) 
# >>> '425291518806427'
print(bill.id) 
# >>> '607559381880556'

The Random primary ID is guaranteed to be unique.

By default the ID will be 16 digits long, but you can change this in settings.py with the RANDOM_ID_MODEL_LENGTH setting.

You can also use RandomAlphaNumIDModel base model to generate string alphanumerical primary keys.

Testing - In Progress

To install django_random_id, along with the tools you need to develop and run the tests, run the following in your virtual environment:

$ pip install -e .[dev]

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_random_id-0.1.0.tar.gz (3.4 kB view details)

Uploaded Source

Built Distribution

django_random_id-0.1.0-py3-none-any.whl (3.6 kB view details)

Uploaded Python 3

File details

Details for the file django_random_id-0.1.0.tar.gz.

File metadata

  • Download URL: django_random_id-0.1.0.tar.gz
  • Upload date:
  • Size: 3.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for django_random_id-0.1.0.tar.gz
Algorithm Hash digest
SHA256 53867bd51b57c93e024a90b525393a82ca72dda85aee445c5a5e08663c6c3f61
MD5 5c60a4342da688f13b2e676c2d15699f
BLAKE2b-256 cc4b8c6354a878e0918881664d1316d0fd832254b8c3d520d7387338a59b3899

See more details on using hashes here.

File details

Details for the file django_random_id-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_random_id-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ef026c5cbee06cb56c1f250434d3d9162d55e1340b4167e5bc0da7f42b65a10e
MD5 f411dac4fb881a455d7af89484a541ab
BLAKE2b-256 90cf7bad67c89b9dc8b0d9edcedb82e7552885b143d570d26e892a02ff199081

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