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. 👶