I
I
Ilya Chichak2019-04-07 16:05:43
Django
Ilya Chichak, 2019-04-07 16:05:43

How to use UUID in url without dash?

I use UUIDField as a primary key in some models, inheriting from such a model:

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

    class Meta:
        abstract = True

urlconfig:
urlpatterns = [
    ...
    path('<pk>/', views.TrainingView.as_view(), name='training_page'),
    ....
]

It is inserted into the URLs for all links in the form 6e0d44f9-7295-4e17-b2d4-9e8a61851c33. I would like it to be in the format 6e0d44f972954e17b2d49e8a61851c33.
The URL dispatcher documentation only says that UUID exists.
Are there any ways to somehow adjust this moment?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
I
Ilya Chichak, 2020-11-03
@ilya_chch

resolved the issue through

force_str(urlsafe_base64_encode(force_bytes(uuid)))

and back to uuid:
force_str(urlsafe_base64_decode(encoded_uid))

D
Dimonchik, 2019-04-07
@dimonchik2013

uuid.uuid4().hex

F
FulTupFul, 2019-04-07
@FulTupFul

def get_default_as_uuid():
    return str(uuid.uuid4()).replace('-', '')

******************
 uuid = models.CharField(max_length=62, default=get_default_as_uuid)
******************

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question