Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create short uuid with Django

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?

like image 258
oybek.t Avatar asked Aug 10 '17 18:08

oybek.t


2 Answers

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

like image 109
Daniel Backman Avatar answered Oct 29 '22 00:10

Daniel Backman


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.

like image 44
ebigood Avatar answered Oct 29 '22 01:10

ebigood