Skip to content

โš™๏ธ ๐ŸŽป

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` &amp; `async def` ๐Ÿ’ช.

๐Ÿšฅ ๐Ÿ‘† ๐Ÿ’ช โ†—๏ธ ๐Ÿ”ƒ ๐Ÿ•โ” โš™๏ธ โ”, โœ… ๐Ÿ‘… ๐Ÿ“„ _"๐Ÿƒ โ“" _ ๐Ÿฉบ ๐Ÿ”ƒ <a href="https://fastapi.tiangolo.com/async/#in-a-hurry" target="_blank" class="internal-link">`async` &amp; `await`</a>.

9๏ธโƒฃ. ๐Ÿ‘‰ โžก ๐Ÿ› ๏ธ ๐Ÿ”ข ๐Ÿšซ ๐Ÿ›ฌ ๐ŸŽป (๐Ÿ‘ โšซ๏ธ ๐Ÿ’ช), โœ‹๏ธ ๐Ÿ“‡ ๐Ÿ“– โฎ๏ธ ๐Ÿ”— ๐Ÿ’ฝ.

FastAPI ๐Ÿ”œ โš™๏ธ `response_model` ๐Ÿ”ข (๐Ÿ‘ˆ ๐Ÿ”Œ ๐ŸŽป) ๐Ÿ—œ ๐Ÿ“จ.

๐Ÿ‘† ๐Ÿ’ช ๐ŸŒ€ dataclasses โฎ๏ธ ๐ŸŽ ๐Ÿ†Ž โœ ๐Ÿ“š ๐ŸŽ ๐ŸŒ€ ๐Ÿ“จ ๐Ÿ— ๐Ÿ“Š ๐Ÿ“Š.

โœ…-๐Ÿ“Ÿ โœ ๐Ÿ’โ€โ™‚ ๐Ÿ”› ๐Ÿ‘€ ๐ŸŒ… ๐ŸŽฏ โ„น.

๐Ÿ’ก ๐ŸŒ…

๐Ÿ‘† ๐Ÿ’ช ๐ŸŒ€ dataclasses โฎ๏ธ ๐ŸŽ Pydantic ๐Ÿท, ๐Ÿ˜– โšช๏ธโžก๏ธ ๐Ÿ‘ซ, ๐Ÿ”Œ ๐Ÿ‘ซ ๐Ÿ‘† ๐Ÿ‘ ๐Ÿท, โ™’๏ธ.

๐Ÿ’ก ๐ŸŒ…, โœ… Pydantic ๐Ÿฉบ ๐Ÿ”ƒ ๐ŸŽป.

โฌ

๐Ÿ‘‰ ๐Ÿ’ช โ†ฉ๏ธ FastAPI โฌ 0.67.0. ๐Ÿ‘ถ