Creating django objects with a random primary key

前端 未结 6 1504
面向向阳花
面向向阳花 2021-02-08 12:08

I\'m working with an API that wants me to generate opaque \"reference IDs\" for transactions with their API, in other words, unique references that users can\'t guess or infer i

6条回答
  •  我在风中等你
    2021-02-08 12:24

    Random integers are not unique, and primary keys must be unique. Make the key field a char(32) and try this instead:

    from uuid import uuid4 as uuid
    randomRef = uuid().hex
    

    UUIDs are very good at providing uniqueness.

提交回复
热议问题