UUID Generator for Python — Code Examples
Quick Answer: Python has a built-in uuid module. Use uuid.uuid4() for random UUIDs. For UUID v7 (time-sorted), use the uuid7 package or Python 3.13+ which adds uuid.uuid7() natively to the standard library.
FAQ
How do I generate a UUID in Python?
import uuid; my_id = uuid.uuid4(). This returns a UUID object. Use str(my_id) for the string representation.
How do I use UUIDs as Django primary keys?
Use models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False). Note: pass uuid.uuid4 without parentheses.