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 hashes)

Uploaded Source

Built Distribution

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

Uploaded Python 3

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