How do you generate a unique, random number to set as the primary key of an object in django?
EDIT – The random number doesn’t have to be the primary key, but it needs to be unique for each object such that I can reference/get/call the object by that number.
Maybe just generate a uuid?
Long uuid’s are meant to avoid collisions and guarantee within a high degree, uniqueness. If you need a shorter id, this will depend on how you intend to use it. If it needs to be unique across all models because you will use it as a root url,
/<slug>/, then it will increase your need to query the database checking for uniqueness before assigning it.You might want to look at using a slugfield, and a snippet like this which sets a unique slug value on model save.
In a nutshell…long UUID == instant value. Shorter slug value == query loop to confirm uniqueness.