Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

使用UUID库生成唯一ID #140

Open
Qingquan-Li opened this issue Dec 16, 2019 · 0 comments
Open

使用UUID库生成唯一ID #140

Qingquan-Li opened this issue Dec 16, 2019 · 0 comments
Labels

Comments

@Qingquan-Li
Copy link
Owner

参考:


实例一: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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant