Generate a Unique String in Python/Django

后端 未结 10 2595
清酒与你
清酒与你 2020-12-15 05:56

What I want is to generate a string(key) of size 5 for my users on my website. More like a BBM PIN.

The key will contain numbers and uppercase English letters:

10条回答
  •  情话喂你
    2020-12-15 06:05

    My favourite is

    import uuid 
    uuid.uuid4().hex[:6].upper()
    

    If you using django you can set the unique constrain on this field in order to make sure it is unique. https://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.Field.unique

提交回复
热议问题