A django model abstract class for creating random int primary keys.
Project description
Django Random ID Model
This module provides a base class for Django models that gives them a random primary key id.
For example, this is the vanilla way to do primary keys:
from django.db import models
class Customer(models.Model):
name = models.CharField(max_length=50)
customer1 = Customer.objects.create(name="John")
customer2 = Customer.objects.create(name="Jane")
print(customer1.id) # '1'
print(customer2.id) # '2'
The primary key just auto increments.
Now use RandomIDModel:
from django.db import models
from django_random_id_model import RandomIDModel
class Customer(RandomIDModel):
name = models.CharField(max_length=50)
customer1 = Customer.objects.create(name="John")
customer2 = Customer.objects.create(name="Jane")
print(customer1.id) # '725393588906066'
print(customer2.id) # '905529381860540'
The ID is guaranteed to be unique.
By default the ID will be 12 digits long, but you can override this in
settings.py with the ID_DIGITS_LENGTH setting.
RandomIDModel inherits directly from models.Model and does not interfere
with anything else, so you can use it wherever you would use models.Model.
Forms and Admin
To integrate your model with a Django ModelForm, you will need to manually
exclude the id field:
from django.forms import ModelForm
class CustomerForm(ModelForm):
class Meta:
model = Customer
exclude = ["id"]
Likewise if you use the Django Admin app:
from django.contrib import admin
class CustomerAdmin(admin.ModelAdmin):
exclude = ["id"]
admin.site.register(Customer, CustomerAdmin)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_random_id_model-0.2.0-py3-none-any.whl.
File metadata
- Download URL: django_random_id_model-0.2.0-py3-none-any.whl
- Upload date:
- Size: 2.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c0fb592272a25dd0b920349c1e4e843b759e367cdd6a540802088e3a82c05c82
|
|
| MD5 |
c3735f0ce5eac37770d2546205400c0d
|
|
| BLAKE2b-256 |
095ba4f1aa6dc520a36c897c8065a1a07b7f5c858c8bc644499be95843e7b69e
|