Reputation: 13329
Im using the django field like this:
import uuid
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
A typical id would look like this:
6abbde08-99b1-42c7-8bd2-3ec92a4b67b9
Is it possible to make the id shorter, to something like 99b1 ?
Upvotes: 5
Views: 5255
Reputation: 47876
I don't think it's a good idea to limit the length of UUIDField
. The whole point of UUIDField
is to generate a unique universal identifier which has no chance that it will generate another UUID
having the same value as an existing one.
If you want to truncate the length, you can do that but it might lead to collisions and you would get 2 same ids
which you certainly don't want.
UUID takes multiple parameters into consideration when generating a random number, so using UUIDField
, you don't need to worry about ever ids
being generated same.
UUID values are 128 bits long and “can guarantee uniqueness across space and time”.
Upvotes: 2
Reputation: 309029
The point of a UUID field is that it is extremely unlikely that two uuids generated will collide. You can't store 99b1
in a UUID
field, because it's not a UUID.
You could use a CharField
with max length 4 as your primary key, but then you will be responsible for generating the primary keys, and making sure that they do not collide.
Upvotes: 4