uuid-utils icon indicating copy to clipboard operation
uuid-utils copied to clipboard

Generate UUID7 from string.

Open stuaxo opened this issue 6 months ago • 2 comments

It would be handy to be able to create a UUID7 from a string, my naive approach of passing one in didn't work + I've seen you need the timestamp itself.

My use case is roundtrips to config (+ checking this).

It's easier to see the difference in some UUIDs themselves than some timestamps (accross a couple of config files, as I have).

I think I also need this to have a UUID7 field in a django app, since that wants to roundtrip to and from string, e.g. in to_python.

stuaxo avatar May 06 '25 11:05 stuaxo

Cheers, got there in the end with help from a chat bot -

For anyone needing this in future, I needed to do:


import uuid_utils as uuid

value = uuid.UUID(value, version=7)

Should probably add this to the README

stuaxocabinetoffice avatar May 06 '25 12:05 stuaxocabinetoffice

In the end I'm not using this library, as I can't work out how to save these into postgres in django in a field.

stuaxo avatar May 06 '25 17:05 stuaxo

>>> import uuid
>>> from uuid_utils import uuid7
>>> uuid.UUID(str(uuid7()))
UUID('0196d5e1-fa88-7993-a921-80946d50987f')

For Python 3.14+, you can probably just do this:

uuid.UUID(uuid7(), version=7)

blag avatar May 15 '25 21:05 blag

For Django specifically you need to use uuid_utils.compat, e.g. this:

from uuid_utils.compat import uuid7

opentyler avatar May 16 '25 17:05 opentyler

Yeah, in addition to what @opentyler mentioned I'm not sure how the documentation can be improved. Feel free to create PRs though

aminalaee avatar May 22 '25 10:05 aminalaee