What This Video Covers
Python UUID tutorial videos walk through the standard uuid module, the new uuid.uuid7() in Python 3.13, and framework integration across Django, SQLAlchemy, Pydantic, and FastAPI.
Skill level: Beginner to intermediate Best for: Python developers using Django, FastAPI, or SQLAlchemy who want to adopt UUID v7
Key Concepts You Will Learn
1. The standard uuid module
import uuid
# UUID v4 — always available
id_v4 = uuid.uuid4()
print(id_v4) # 550e8400-e29b-41d4-a716-446655440000
print(type(id_v4)) # <class 'uuid.UUID'>
print(id_v4.hex) # 32-char hex, no hyphens
print(str(id_v4)) # standard hyphenated string
2. UUID v7 in Python 3.13+
import uuid
# Python 3.13+ — standard library
id_v7 = uuid.uuid7()
print(id_v7) # 018fbe3a-4c5d-7b12-8abc-0123456789ab
# Extracting the timestamp
ts_ms = int(str(id_v7).replace('-', '')[:12], 16)
from datetime import datetime, timezone
dt = datetime.fromtimestamp(ts_ms / 1000, tz=timezone.utc)
For Python 3.9–3.12, see the uuid6 backport package.
3. Django UUIDField
# models.py — Python 3.13+
import uuid
from django.db import models
class Order(models.Model):
id = models.UUIDField(
primary_key=True,
default=uuid.uuid7, # callable — called on each new instance
editable=False,
)
status = models.CharField(max_length=50)
No migration needed to switch from uuid.uuid4 to uuid.uuid7 — the UUIDField column type stores both identically.
4. SQLAlchemy 2.x
from sqlalchemy import Column
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.orm import DeclarativeBase
import uuid
class Base(DeclarativeBase): pass
class Order(Base):
__tablename__ = 'orders'
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid7)
For PostgreSQL 18 server-side generation: server_default=text('uuidv7()').
5. FastAPI with Pydantic v2
from fastapi import FastAPI
from pydantic import BaseModel
import uuid
app = FastAPI()
class OrderCreate(BaseModel):
id: uuid.UUID = uuid.uuid7() # Python 3.13+
@app.post('/orders')
def create_order(order: OrderCreate):
return {'id': str(order.id)}
Pydantic v2 validates uuid.UUID objects automatically — any version is accepted at the type level.
Related Reading
- UUID in Python — full guide
- Python 3.13 adds uuid.uuid7()
- UUID in Databases — SQLAlchemy storage
- UUID v7 ORM support in 2026
Watch on YouTube
Search for Python UUID tutorial videos on YouTube ↗
Generate a UUID Now
- Generate a UUID v7 — time-sortable, Python 3.13 compatible
- UUID Decoder — extract the timestamp from any UUID v7
Frequently asked questions
What is the best way to learn how UUID v7 works?
Start with the bit layout: UUID v7 puts a 48-bit Unix millisecond timestamp in the leading bits, followed by a 4-bit version (7), a 12-bit monotonic counter, the 2-bit variant, and 62 random bits. The timestamp at the front is why v7 sorts chronologically as a plain string — key to B-tree performance. See the UUID Byte Layout guide and the UUID v7 Explained walkthrough.
How do I generate UUID v7 in Python?
In Python 3.13+, use uuid.uuid7() from the standard library. For Python 3.9–3.12, install the uuid6 package (pip install uuid6) and use uuid6.uuid7(). Both return a uuid.UUID object with the same interface as uuid.uuid4().
What column type should I use to store a UUID in a database?
Use the native type where one exists — uuid in PostgreSQL, uniqueidentifier in SQL Server. In MySQL use BINARY(16) and store raw bytes. Avoid VARCHAR(36): it costs 36 bytes per row versus 16 for binary and makes every comparison a string operation.
How do I generate UUID v7 in Python 3.13?
Python 3.13 added uuid.uuid7() to the standard library — no third-party package needed. For Python 3.9–3.12, install the uuid6 package (pip install uuid6) and call uuid6.uuid7(). Both return a uuid.UUID object with the same interface as uuid.uuid4(). See Python 3.13 adds uuid.uuid7().
How do I use UUID v7 as a primary key in Django?
In Django, set the model's id field to UUIDField(primary_key=True, default=uuid.uuid7) on Python 3.13+, or default=uuid6.uuid7 with the uuid6 package on earlier versions. The existing UUIDField stores any UUID version as a 128-bit value — no schema migration needed when switching from v4. See UUID in Python for full Django and SQLAlchemy examples.