I am making a web application in Django. I want to use custom id field in my model and i know about uuid module. The problem is i don't know where to put this logic. I don't want to use Django's AutoField. I want it to be such that if one row is entered then this id field must be custom not AutoField. A silly idea comes to my mind is to change the id field later after row insertion with my custom id. Can anyone help me to sort this problem out. Any assistance is highly appreciated.
Asked
Active
Viewed 3.4k times
14
-
1Could you please provide an example of what you want your ID values be? Thanks. – alecxe Jan 31 '16 at 03:21
-
For user model i would like to use id as user+randomly generated key. For example X user id should be "user12djghdhgdbxbfhdty". This makes easy to identify that this id is for a user and is sufficiently large enough to allow sufficient users. – user2719152 Jan 31 '16 at 03:41
-
Take a look at [JPG solution](https://stackoverflow.com/questions/52070462/django-generate-custom-id) and combine it with UUID. – Memória de Cálculo Mar 07 '19 at 00:31
2 Answers
27
https://docs.djangoproject.com/en/dev/topics/db/models/#automatic-primary-key-fields
If you’d like to specify a custom primary key, just specify primary_key=True on one of your fields. If Django sees you’ve explicitly set Field.primary_key, it won’t add the automatic id column.
So you want (from https://docs.djangoproject.com/en/dev/ref/models/fields/#uuidfield)
import uuid
from django.db import models
class MyUUIDModel(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
# other fields
you don't have to name the field id
I think.

eugene
- 39,839
- 68
- 255
- 489
-
-
Hi is there any possibility that the uuid4 give 2 identical ids? – Fabio Magarelli Oct 01 '20 at 13:07
-
5@FabioMagarelli theoretically yes, but it has 16 random bytes, so practically you don't have to worry about it. To get 50% collision chance (ie. two uuids having the same value) you would have to generate 1 billion uuids every second for 85 years. – Iftah Oct 18 '20 at 23:26
-
1@FabioMagarelli Also, because the `default` parameters takes a function, you could write your own function that generates and returns a uuid that doesn't collide with an existing one in the database. (This introduces some overhead of course). – Mathieu Dhondt Mar 31 '22 at 11:17
0
On my project, this is what I did. Supposed that you have a sample model with a custom id and the autofield ID:
class Product(models.Model)
product_tag_number = models.CharField(max_length=50, editable=False, unique=True) # this is my custom ID
def save(self, *args, **kwargs): # I override my save function in here.
super().save(*args, **kwargs)
self.set_ptn() # And then call my own function in here to generate such custom ID combining the autoField ID and the UUID.
def set_ptn(self): # And this is the function for generating the custom ID for the item
......
And then your function goes here for combining them
......
Note: Doing this method will not affect and will not use the AutoField generated by Django for you. But it's your call if you will use this as your primary key.

dev_ren
- 31
- 7