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 <mobile@aldian.net> kirjoitti:
--Hi, please try the code above.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
)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-users+unsubscribe@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,Aldian Fazrihady
http://aldianfazrihady.com
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAN7EoAYykEHF8qxAEc%2BPh71ue%3DKLzpT3P5MuE2Dw58gG2HmUEw%40mail.gmail.com.
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAHn91oc4scdb1Vd5W9_pifq128kE87PRJ-ac_oiQbCe3QEMi5w%40mail.gmail.com.
No comments:
Post a Comment