https://github.com/samirelanduk/django-random-id-model
A model base class which creates long, random, integer primary keys.
https://github.com/samirelanduk/django-random-id-model
django django-models id-generator primary-key
Last synced: about 1 year ago
JSON representation
A model base class which creates long, random, integer primary keys.
- Host: GitHub
- URL: https://github.com/samirelanduk/django-random-id-model
- Owner: samirelanduk
- Created: 2020-11-14T18:51:25.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2024-02-12T10:49:35.000Z (over 2 years ago)
- Last Synced: 2025-02-28T23:46:32.204Z (over 1 year ago)
- Topics: django, django-models, id-generator, primary-key
- Language: Python
- Homepage:
- Size: 7.81 KB
- Stars: 8
- Watchers: 3
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
Awesome Lists containing this project
README
# 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:
```python
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`:
```python
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:
```python
from django.forms import ModelForm
class CustomerForm(ModelForm):
class Meta:
model = Customer
exclude = ["id"]
```
Likewise if you use the Django Admin app:
```python
from django.contrib import admin
class CustomerAdmin(admin.ModelAdmin):
exclude = ["id"]
admin.site.register(Customer, CustomerAdmin)
```