Qingquan-Li / blog

My Blog
https://Qingquan-Li.github.io/blog/
132 stars 16 forks source link

使用UUID库生成唯一ID #140

Open Qingquan-Li opened 4 years ago

Qingquan-Li commented 4 years ago

参考:


实例一:Python 使用 UUID 库生成随机 ID

$ python3
Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 26 2016, 10:47:25)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import uuid
>>> # make a random UUID
>>> # 基于伪随机数生成,有可能重复,重复概率极低。
>>> uuid.uuid4()
UUID('16fd2706-8baf-433b-82eb-8c7fada847da')

UUID是如何保证唯一性的

UUID (uuid4)重复概率

uuid唯一性


实例二:Django 为用户生成 UUID

参考:

import uuid
from django.db import models

class Customer(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)