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
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
)
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.
pe 4. lokak. 2019 klo 18.12 Aldian Fazrihady kirjoitti:
> import random
> import string
>
> from django.db import models
>
>
> def gener
Use uuid
On Fri, 4 Oct, 2019, 8:23 PM yashwanth .k, 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 rando
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.
https://github.com/speedy-net/speedy-net
https://github.com/speedy-net/speedy-net/blob
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.
On Fri, Oct 4, 2019 at 8:41 PM Aldian Fazrihady wrote:
> import random
> import string
>
> from django.db import m
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.
>
7 matches
Mail list logo