How to create short uuid with Django

Viewed 6848

I have a model and UUID as primary key field. UUID id is way too long. I'd like to have it short similar to YouTube.

class Video(models.Model):
    id = models.UUIDField(
        primary_key=True, 
        default=uuid.uuid4, 
        editable=False,
    )

Instead of this

UUID('b39596dd-10c9-42c9-91ee-9e45b78ce3c1')

I want to have just this

UUID('b39596dd')

How can I achieve above?

3 Answers

A bit old question, but shortuuidfield may be what you are looking for.

You can use shortuuid library:

from shortuuid.django_fields import ShortUUIDField
id = ShortUUIDField(primary_key=True, length=11, max_length=11)

But you should pay attention to this issue. The smaller the size of the UUID, the higher the probability of Collision.

Related