Tuesday, 5 March 2024

python django uuid

https://stackoverflow.com/questions/1785503/when-should-i-use-uuid-uuid1-vs-uuid-uuid4-in-python

uuid1():
Generate a UUID from a host ID, sequence number, and the current time

uuid4():

Generate a random UUID.


uuid1() is guaranteed to not produce any collisions (under the assumption you do not create too many of them at the same time). I wouldn't use it if it's important that there's no connection between the uuid and the computer, as the mac address gets used to make it unique across computers.

You can create duplicates by creating more than 214 uuid1 in less than 100ns, but this is not a problem for most use cases.

uuid4() generates, as you said, a random UUID. The chance of a collision is really, really, really small. Small enough, that you shouldn't worry about it. The problem is, that a bad random-number generator makes it more likely to have collisions.


 


get string version of uuid and return to front end 


https://stackoverflow.com/questions/60183516/how-to-create-uuid-with-dashes-instead-of-without-dashes-using-uuid-package-for

 import uuid
>>> u = str(uuid.uuid1())
>>> u
'7d7b626c-4d6d-11ea-989f-784f435149ee'

when front end supplies string uuid, perform look up in data base (ex '7d7b626c-4d6d-11ea-989f-784f435149ee')
 uuid.UUID(str(val))
// if you are certain val is string then 
 uuid.UUID(val)

// this converts into uuid class object0

// above is enough to use in query set
// to get string representation of what is stored in db
>>> id =  uuid.uuid4().hex
>>> id
'daf59b684d6a11xz9a9c34028611c679'


https://stackoverflow.com/questions/15859156/python-how-to-convert-a-valid-uuid-from-string-to-uuid
check if uuid is valid
def is_valid_uuid(val):
try:
    uuid.UUID(str(val))
    return True
except ValueError:
    return False

No comments:

Post a Comment