how to make default uuid or id for pydantic model

PHOTO EMBED

Sun Jul 21 2024 09:04:01 GMT+0000 (Coordinated Universal Time)

Saved by @freepythoncode ##python #coding #python #programming #pydantic #uuid

from pydantic import BaseModel, Field
from uuid import UUID, uuid4

class item(BaseModel):
    name : str
    price : float
    item_id : UUID = Field(default_factory = uuid4)


items = []

for i in range(3):
    items.append(item(name = 'test', price = 20))


print(items)
content_copyCOPY