Discussion:
Encode and shorten UUIDs in URLs
t***@adaptive-channel.com
2018-08-23 13:58:29 UTC
Permalink
We are building an api which exposes uuids instead of pks to access objects.

As a way to shorten the urls and make them friendlier, we thought we could
encode the uuids in a higher base than the default base16 (we chose base58
for readability if you're curious, but that doesn't matter).

What's the best approach to *decode and encode uuids seemlessly** in urls,
as they go in and out of our api?*

Also, we would of course like for reverse() to yield urls with encoded
uuids.

After skimming through Django rest framework's source code, I've tried
customizing serializers, middleware, views, etc. without success and I'm a
bit lost now.
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Chris Foresman
2018-08-24 13:13:56 UTC
Permalink
It almost sounds like you'd need to patch Django's URL resolver to look for capture groups that might be a primary key, and then base58 decode the value before passing it as a value to the view method being called. Then you would need to write your own function, `my_reverse`, which would look for kwargs that would be primary keys, and base58 encode them before passing them on to reverse and then returning that.
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
gordon
2018-08-24 13:17:36 UTC
Permalink
You could also make a custom uuid field that handles this. Use a uuid
subclass that uses base 58 for string conversion. And then convert to
correct database representation in the field methods
Post by Chris Foresman
It almost sounds like you'd need to patch Django's URL resolver to look
for capture groups that might be a primary key, and then base58 decode the
value before passing it as a value to the view method being called. Then
you would need to write your own function, `my_reverse`, which would look
for kwargs that would be primary keys, and base58 encode them before
passing them on to reverse and then returning that.
--
You received this message because you are subscribed to the Google Groups
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Chris Foresman
2018-08-24 13:20:50 UTC
Permalink
Yes, this is a better idea than mine.
Post by gordon
You could also make a custom uuid field that handles this. Use a uuid
subclass that uses base 58 for string conversion. And then convert to
correct database representation in the field methods
Post by Chris Foresman
It almost sounds like you'd need to patch Django's URL resolver to look
for capture groups that might be a primary key, and then base58 decode the
value before passing it as a value to the view method being called. Then
you would need to write your own function, `my_reverse`, which would look
for kwargs that would be primary keys, and base58 encode them before
passing them on to reverse and then returning that.
--
You received this message because you are subscribed to the Google Groups
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to a topic in the
Google Groups "Django REST framework" group.
To unsubscribe from this topic, visit
https://groups.google.com/d/topic/django-rest-framework/ePEfWaamnxA/unsubscribe
.
To unsubscribe from this group and all its topics, send an email to
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...