Pydantic 모델을 이용하여 Nested Model도 구현할 수 있다.

List Fields

class Item(BaseModel):
    name: str
    description: Optional[str] = None
    price: float
    tax: Optional[float] = None
    tags: list = []

tags는 어떠한 자료형이든 담을 수 있는 list가 된다.

from typing import List, Optional

tags: List[str] = []

위와 같이 typing에서 List를 import하여 List[str]과 같이 쓰면, str만이 요소가 되는 리스트가 된다.

Set

Set 또한 내부 요소를 제한할 수 있다.

from typing import Optional, Set

class Item(BaseModel):
    name: str
    description: Optional[str] = None
    price: float
    tax: Optional[float] = None
    tags: Set[str] = set()

Nested Models

Pydantic으로 정의한 모델 또한 하나의 자료형이 되기에 다른 Pydantic 모델의 type hint에 쓰일 수 있다.

class Image(BaseModel):
    url: str
    name: str

class Item(BaseModel):
    name: str
    description: Optional[str] = None
    price: float
    tax: Optional[float] = None
    tags: Set[str] = []
    image: Optional[Image] = None

@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item):
    results = {"item_id": item_id, "item": item}
    return results

먼저 정의된 Image모델이 Item모델의 item변수의 type으로 쓰이는 경우이다.

이렇게 되면 아래와 같은 구조의 body가 들어와야한다.

{
    "name": "Foo",
    "description": "The pretender",
    "price": 42.0,
    "tax": 3.2,
    "tags": ["rock", "metal", "bar"],
    "image": {
        "url": "<http://example.com/baz.jpg>",
        "name": "The Foo live"
    }
}

Deeper Nested Model