# 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)
```
Raw data
{
"_id": null,
"home_page": "https://github.com/samirelanduk/django-random-id-model",
"name": "django-random-id-model",
"maintainer": "",
"docs_url": null,
"requires_python": "!=2.*, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*, !=3.5.*",
"maintainer_email": "",
"keywords": "django orm primary-key",
"author": "Sam Ireland",
"author_email": "mail@samireland.com",
"download_url": "",
"platform": null,
"description": "# Django Random ID Model\n\nThis module provides a base class for Django models that gives them a random\nprimary key id.\n\nFor example, this is the vanilla way to do primary keys:\n\n```python\nfrom django.db import models\n\nclass Customer(models.Model):\n name = models.CharField(max_length=50)\n\ncustomer1 = Customer.objects.create(name=\"John\")\ncustomer2 = Customer.objects.create(name=\"Jane\")\nprint(customer1.id) # '1'\nprint(customer2.id) # '2'\n```\n\nThe primary key just auto increments.\n\nNow use `RandomIDModel`:\n\n```python\nfrom django.db import models\nfrom django_random_id_model import RandomIDModel\n\nclass Customer(RandomIDModel):\n name = models.CharField(max_length=50)\n\ncustomer1 = Customer.objects.create(name=\"John\")\ncustomer2 = Customer.objects.create(name=\"Jane\")\nprint(customer1.id) # '725393588906066'\nprint(customer2.id) # '905529381860540'\n```\n\nThe ID is guaranteed to be unique.\n\nBy default the ID will be 12 digits long, but you can override this in\nsettings.py with the `ID_DIGITS_LENGTH` setting.\n\n`RandomIDModel` inherits directly from `models.Model` and does not interfere\nwith anything else, so you can use it wherever you would use `models.Model`.\n\n## Forms and Admin\n\nTo integrate your model with a Django ModelForm, you will need to manually\nexclude the `id` field:\n\n```python\nfrom django.forms import ModelForm\n\nclass CustomerForm(ModelForm):\n\n class Meta:\n model = Customer\n exclude = [\"id\"]\n```\n\nLikewise if you use the Django Admin app:\n\n```python\nfrom django.contrib import admin\n\nclass CustomerAdmin(admin.ModelAdmin):\n exclude = [\"id\"]\n\nadmin.site.register(Customer, CustomerAdmin)\n```\n\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "A django model abstract class for creating random int primary keys.",
"version": "0.2.0",
"project_urls": {
"Homepage": "https://github.com/samirelanduk/django-random-id-model"
},
"split_keywords": [
"django",
"orm",
"primary-key"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "095ba4f1aa6dc520a36c897c8065a1a07b7f5c858c8bc644499be95843e7b69e",
"md5": "c3735f0ce5eac37770d2546205400c0d",
"sha256": "c0fb592272a25dd0b920349c1e4e843b759e367cdd6a540802088e3a82c05c82"
},
"downloads": -1,
"filename": "django_random_id_model-0.2.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "c3735f0ce5eac37770d2546205400c0d",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "!=2.*, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*, !=3.5.*",
"size": 2776,
"upload_time": "2024-02-12T10:48:10",
"upload_time_iso_8601": "2024-02-12T10:48:10.545940Z",
"url": "https://files.pythonhosted.org/packages/09/5b/a4f1aa6dc520a36c897c8065a1a07b7f5c858c8bc644499be95843e7b69e/django_random_id_model-0.2.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-02-12 10:48:10",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "samirelanduk",
"github_project": "django-random-id-model",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "django-random-id-model"
}