How to create a GUID/UUID in Python

前端 未结 8 1111
情话喂你
情话喂你 2020-11-22 16:47

How do I create a GUID in Python that is platform independent? I hear there is a method using ActivePython on Windows but it\'s Windows only because it uses COM. Is there a

8条回答
  •  长情又很酷
    2020-11-22 17:28

    If you need to pass UUID for a primary key for your model or unique field then below code returns the UUID object -

     import uuid
     uuid.uuid4()
    

    If you need to pass UUID as a parameter for URL you can do like below code -

    import uuid
    str(uuid.uuid4())
    

    If you want the hex value for a UUID you can do the below one -

    import uuid    
    uuid.uuid4().hex
    

提交回复
热议问题