How to generate a unique 9 digit random id for every registered user?

916 views
Skip to first unread message

yashwanth .k

unread,
Oct 4, 2019, 10:51:52 AM10/4/19
to Django users
Hello,

My model consists of user details such as username and his id which is auto-generated by DB(indexing). 
Apart from this, I need to assign a random unique 9 digit number for every user.
The reason for assigning a random number to a registered user is to search a particular user using his permanent random id which we have generated.

Solutions with the model description will be very helpful.


Thank you!

Aldian Fazrihady

unread,
Oct 4, 2019, 11:12:09 AM10/4/19
to django...@googlegroups.com
import random
import string

from django.db import models


def generate_random_code():
return ''.join(random.choices(string.ascii_uppercase + string.digits, k=
9))


class MyModel(models.Model):
code = models.CharField(
max_length=9, default=generate_random_code, db_index=True
)
Hi, please try the code above.
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/8eb92ef2-ffe0-431d-b4aa-3d6c5a119c3f%40googlegroups.com.


--
Regards,

Jani Tiainen

unread,
Oct 4, 2019, 11:54:51 AM10/4/19
to django...@googlegroups.com
Note that above solution doesn't guarantee unique id. As it is a problem of any pseudorandom generator.

So it is possible (but unlikely) that insertion fails.

Suraj Thapa FC

unread,
Oct 4, 2019, 11:59:54 PM10/4/19
to django...@googlegroups.com
Use uuid 

--

אורי

unread,
Oct 5, 2019, 12:11:22 AM10/5/19
to django...@googlegroups.com
Hi,

In Speedy Net we generate a unique random ID with 15 digits and this is used as the table's primary key. We don't use the DB's auto-generated ID.

The first digit of the random ID we generate is never zero.

 

--

yashwanth .k

unread,
Oct 5, 2019, 1:59:05 AM10/5/19
to django...@googlegroups.com
Hello Aldian Fazrihady, 
Your code is working, 
How to link this code to my user so that when I search the code I should grab user profile.
Thank you so much for sharing the approach.

yashwanth .k

unread,
Oct 5, 2019, 2:01:54 AM10/5/19
to Django users
Hello Uri, 
Thank you soo much,
your work made my work easy.
Thank you!

On Saturday, October 5, 2019 at 9:41:22 AM UTC+5:30, Uri wrote:
Hi,

In Speedy Net we generate a unique random ID with 15 digits and this is used as the table's primary key. We don't use the DB's auto-generated ID.

The first digit of the random ID we generate is never zero.

 

On Fri, Oct 4, 2019 at 5:53 PM yashwanth .k <kyashw...@gmail.com> wrote:
Hello,

My model consists of user details such as username and his id which is auto-generated by DB(indexing). 
Apart from this, I need to assign a random unique 9 digit number for every user.
The reason for assigning a random number to a registered user is to search a particular user using his permanent random id which we have generated.

Solutions with the model description will be very helpful.


Thank you!

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages