โ๏ธ ๐ป¶
FastAPI ๐ ๐ ๐ Pydantic, & ๐ค โ๏ธ ๐ ๐ โ โ๏ธ Pydantic ๐ท ๐ฃ ๐จ & ๐จ.
โ๏ธ FastAPI ๐โ๐ฆบ โ๏ธ dataclasses ๐ ๐:
from dataclasses import dataclass
from typing import Union
from fastapi import FastAPI
@dataclass
class Item:
    name: str
    price: float
    description: Union[str, None] = None
    tax: Union[float, None] = None
app = FastAPI()
@app.post("/items/")
async def create_item(item: Item):
    return item
๐ ๐โ๐ฆบ ๐ Pydantic, โซ๏ธ โ๏ธ ๐ ๐โ๐ฆบ dataclasses.
, โฎ๏ธ ๐ ๐ ๐ ๐ซ โ๏ธ Pydantic ๐ฏ, FastAPI โ๏ธ Pydantic ๐ ๐ ๐ฉ ๐ป Pydantic ๐ ๐ ๐ป.
& โ๏ธ, โซ๏ธ ๐โ๐ฆบ ๐:
- ๐ฝ ๐ฌ
 - ๐ฝ ๐ ๏ธ
 - ๐ฝ ๐งพ, โ๏ธ.
 
๐ ๐ท ๐ ๐ โฎ๏ธ Pydantic ๐ท. & โซ๏ธ ๐ค ๐ ๐ ๐ ๐, โ๏ธ Pydantic.
Info
โ๏ธ ๐คฏ ๐ ๐ป ๐ช ๐ซ ๐ Pydantic ๐ท ๐ช.
, ๐ 5๏ธโฃ๐ ๐ช โ๏ธ Pydantic ๐ท.
โ๏ธ ๐ฅ ๐ โ๏ธ ๐ ๐ป ๐คฅ ๐คญ, ๐ ๐ ๐ฑ โ๏ธ ๐ซ ๐๏ธ ๐ธ ๐ ๏ธ โ๏ธ FastAPI. ๐ถ
๐ป response_model¶
๐ ๐ช โ๏ธ dataclasses response_model ๐ข:
from dataclasses import dataclass, field
from typing import List, Union
from fastapi import FastAPI
@dataclass
class Item:
    name: str
    price: float
    tags: List[str] = field(default_factory=list)
    description: Union[str, None] = None
    tax: Union[float, None] = None
app = FastAPI()
@app.get("/items/next", response_model=Item)
async def read_next_item():
    return {
        "name": "Island In The Moon",
        "price": 12.99,
        "description": "A place to be be playin' and havin' fun",
        "tags": ["breater"],
    }
๐ป ๐ ๐ ๐ Pydantic ๐ป.
๐ ๐, ๐ฎ ๐ ๐ ๐ฆ ๐ ๐ ๏ธ ๐ฉบ ๐ฉโ๐ป ๐ข:

๐ป ๐ ๐ ๐¶
๐ ๐ช ๐ dataclasses โฎ๏ธ ๐ ๐ โ โ ๐ฆ ๐ ๐.
๐ผ, ๐ ๐ช โ๏ธ โ๏ธ Pydantic โฌ dataclasses. ๐ผ, ๐ฅ ๐ โ๏ธ โ โฎ๏ธ ๐ ๐ ๐ ๏ธ ๐งพ.
๐ ๐ผ, ๐ ๐ช ๐ฏ ๐ฑ ๐ฉ dataclasses โฎ๏ธ pydantic.dataclasses, โ ๐ง-โป:
from dataclasses import field  # (1)
from typing import List, Union
from fastapi import FastAPI
from pydantic.dataclasses import dataclass  # (2)
@dataclass
class Item:
    name: str
    description: Union[str, None] = None
@dataclass
class Author:
    name: str
    items: List[Item] = field(default_factory=list)  # (3)
app = FastAPI()
@app.post("/authors/{author_id}/items/", response_model=Author)  # (4)
async def create_author_items(author_id: str, items: List[Item]):  # (5)
    return {"name": author_id, "items": items}  # (6)
@app.get("/authors/", response_model=List[Author])  # (7)
def get_authors():  # (8)
    return [  # (9)
        {
            "name": "Breaters",
            "items": [
                {
                    "name": "Island In The Moon",
                    "description": "A place to be be playin' and havin' fun",
                },
                {"name": "Holy Buddies"},
            ],
        },
        {
            "name": "System of an Up",
            "items": [
                {
                    "name": "Salt",
                    "description": "The kombucha mushroom people's favorite",
                },
                {"name": "Pad Thai"},
                {
                    "name": "Lonely Night",
                    "description": "The mostests lonliest nightiest of allest",
                },
            ],
        },
    ]
1๏ธโฃ. ๐ฅ ๐ field โช๏ธโก๏ธ ๐ฉ dataclasses.
2๏ธโฃ. pydantic.dataclasses ๐ง-โป dataclasses.
3๏ธโฃ. Author ๐ป ๐ ๐ Item ๐ป.
4๏ธโฃ. Author ๐ป โ๏ธ response_model ๐ข.
5๏ธโฃ. ๐ ๐ช โ๏ธ ๐ ๐ฉ ๐ โ โฎ๏ธ ๐ป ๐จ ๐ช.
๐ ๐ผ, โซ๏ธ ๐ `Item` ๐ป.
6๏ธโฃ. ๐ฅ ๐ฅ ๐ฌ ๐ ๐ ๐ items โ ๐ ๐ป.
FastAPI ๐ฏ <abbr title="converting the data to a format that can be transmitted">โ</abbr> ๐ฝ ๐ป.
7๏ธโฃ. ๐ฅ response_model โ๏ธ ๐ โ ๐ Author ๐ป.
๐, ๐ ๐ช ๐ `dataclasses` โฎ๏ธ ๐ฉ ๐ โ.
8๏ธโฃ. ๐ ๐ ๐ โก ๐ ๏ธ ๐ข โ๏ธ ๐ฅ def โฉ๏ธ async def.
๐ง, FastAPI ๐ ๐ช ๐ `def` & `async def` ๐ช.
๐ฅ ๐ ๐ช โ๏ธ ๐ ๐โ โ๏ธ โ, โ
 ๐
 ๐ _"๐ โ" _ ๐ฉบ ๐ <a href="https://fastapi.tiangolo.com/async/#in-a-hurry" target="_blank" class="internal-link">`async` & `await`</a>.
9๏ธโฃ. ๐ โก ๐ ๏ธ ๐ข ๐ซ ๐ฌ ๐ป (๐ โซ๏ธ ๐ช), โ๏ธ ๐ ๐ โฎ๏ธ ๐ ๐ฝ.
FastAPI ๐ โ๏ธ `response_model` ๐ข (๐ ๐ ๐ป) ๐ ๐จ.
๐ ๐ช ๐ dataclasses โฎ๏ธ ๐ ๐ โ ๐ ๐ ๐ ๐จ ๐ ๐ ๐.
โ -๐ โ ๐โโ ๐ ๐ ๐ ๐ฏ โน.
๐ก ๐ ¶
๐ ๐ช ๐ dataclasses โฎ๏ธ ๐ Pydantic ๐ท, ๐ โช๏ธโก๏ธ ๐ซ, ๐ ๐ซ ๐ ๐ ๐ท, โ๏ธ.
๐ก ๐ , โ Pydantic ๐ฉบ ๐ ๐ป.
โฌ¶
๐ ๐ช โฉ๏ธ FastAPI โฌ 0.67.0. ๐ถ