Skip to content

Latest commit

 

History

History
481 lines (296 loc) · 16 KB

response-model.md

File metadata and controls

481 lines (296 loc) · 16 KB

📨 🏷 - 📨 🆎

👆 💪 📣 🆎 ⚙️ 📨 ✍ ➡ 🛠️ 🔢 📨 🆎.

👆 💪 ⚙️ 🆎 ✍ 🎏 🌌 👆 🔜 🔢 💽 🔢 🔢, 👆 💪 ⚙️ Pydantic 🏷, 📇, 📖, 📊 💲 💖 🔢, 🎻, ♒️.

=== "🐍 3️⃣.6️⃣ & 🔛"

```Python hl_lines="18  23"
{!> ../../../docs_src/response_model/tutorial001_01.py!}
```

=== "🐍 3️⃣.9️⃣ & 🔛"

```Python hl_lines="18  23"
{!> ../../../docs_src/response_model/tutorial001_01_py39.py!}
```

=== "🐍 3️⃣.1️⃣0️⃣ & 🔛"

```Python hl_lines="16  21"
{!> ../../../docs_src/response_model/tutorial001_01_py310.py!}
```

FastAPI 🔜 ⚙️ 👉 📨 🆎:

  • 📨 💽.
    • 🚥 💽 ❌ (✅ 👆 ❌ 🏑), ⚫️ ⛓ 👈 👆 📱 📟 💔, 🚫 🛬 ⚫️❔ ⚫️ 🔜, & ⚫️ 🔜 📨 💽 ❌ ↩️ 🛬 ❌ 💽. 👉 🌌 👆 & 👆 👩‍💻 💪 🎯 👈 👫 🔜 📨 💽 & 💽 💠 📈.
  • 🚮 🎻 🔗 📨, 🗄 ➡ 🛠️.
    • 👉 🔜 ⚙️ 🏧 🩺.
    • ⚫️ 🔜 ⚙️ 🏧 👩‍💻 📟 ⚡ 🧰.

✋️ 🏆 🥈:

  • ⚫️ 🔜 📉 & ⛽ 🔢 📊 ⚫️❔ 🔬 📨 🆎.
    • 👉 ✴️ ⚠ 💂‍♂, 👥 🔜 👀 🌅 👈 🔛.

response_model 🔢

📤 💼 🌐❔ 👆 💪 ⚖️ 💚 📨 💽 👈 🚫 ⚫️❔ ⚫️❔ 🆎 📣.

🖼, 👆 💪 💚 📨 📖 ⚖️ 💽 🎚, ✋️ 📣 ⚫️ Pydantic 🏷. 👉 🌌 Pydantic 🏷 🔜 🌐 💽 🧾, 🔬, ♒️. 🎚 👈 👆 📨 (✅ 📖 ⚖️ 💽 🎚).

🚥 👆 🚮 📨 🆎 ✍, 🧰 & 👨‍🎨 🔜 😭 ⏮️ (☑) ❌ 💬 👆 👈 👆 🔢 🛬 🆎 (✅#️⃣) 👈 🎏 ⚪️➡️ ⚫️❔ 👆 📣 (✅ Pydantic 🏷).

📚 💼, 👆 💪 ⚙️ ➡ 🛠️ 👨‍🎨 🔢 response_model ↩️ 📨 🆎.

👆 💪 ⚙️ response_model 🔢 🙆 ➡ 🛠️:

  • @app.get()
  • @app.post()
  • @app.put()
  • @app.delete()
  • ♒️.

=== "🐍 3️⃣.6️⃣ & 🔛"

```Python hl_lines="17  22  24-27"
{!> ../../../docs_src/response_model/tutorial001.py!}
```

=== "🐍 3️⃣.9️⃣ & 🔛"

```Python hl_lines="17  22  24-27"
{!> ../../../docs_src/response_model/tutorial001_py39.py!}
```

=== "🐍 3️⃣.1️⃣0️⃣ & 🔛"

```Python hl_lines="17  22  24-27"
{!> ../../../docs_src/response_model/tutorial001_py310.py!}
```

!!! note 👀 👈 response_model 🔢 "👨‍🎨" 👩‍🔬 (get, post, ♒️). 🚫 👆 ➡ 🛠️ 🔢, 💖 🌐 🔢 & 💪.

response_model 📨 🎏 🆎 👆 🔜 📣 Pydantic 🏷 🏑,, ⚫️ 💪 Pydantic 🏷, ✋️ ⚫️ 💪, ✅ list Pydantic 🏷, 💖 List[Item].

FastAPI 🔜 ⚙️ 👉 response_model 🌐 💽 🧾, 🔬, ♒️. & 🗜 & ⛽ 🔢 📊 🚮 🆎 📄.

!!! tip 🚥 👆 ✔️ ⚠ 🆎 ✅ 👆 👨‍🎨, ✍, ♒️, 👆 💪 📣 🔢 📨 🆎 Any.

👈 🌌 👆 💬 👨‍🎨 👈 👆 😫 🛬 🕳. ✋️ FastAPI 🔜 💽 🧾, 🔬, 🖥, ♒️. ⏮️ `response_model`.

response_model 📫

🚥 👆 📣 👯‍♂️ 📨 🆎 & response_model, response_model 🔜 ✊ 📫 & ⚙️ FastAPI.

👉 🌌 👆 💪 🚮 ☑ 🆎 ✍ 👆 🔢 🕐❔ 👆 🛬 🆎 🎏 🌘 📨 🏷, ⚙️ 👨‍🎨 & 🧰 💖 ✍. & 👆 💪 ✔️ FastAPI 💽 🔬, 🧾, ♒️. ⚙️ response_model.

👆 💪 ⚙️ response_model=None ❎ 🏗 📨 🏷 👈 ➡ 🛠️, 👆 5️⃣📆 💪 ⚫️ 🚥 👆 ❎ 🆎 ✍ 👜 👈 🚫 ☑ Pydantic 🏑, 👆 🔜 👀 🖼 👈 1️⃣ 📄 🔛.

📨 🎏 🔢 💽

📥 👥 📣 UserIn 🏷, ⚫️ 🔜 🔌 🔢 🔐:

=== "🐍 3️⃣.6️⃣ & 🔛"

```Python hl_lines="9  11"
{!> ../../../docs_src/response_model/tutorial002.py!}
```

=== "🐍 3️⃣.1️⃣0️⃣ & 🔛"

```Python hl_lines="7  9"
{!> ../../../docs_src/response_model/tutorial002_py310.py!}
```

!!! info ⚙️ EmailStr, 🥇 ❎ email_validator.

🤶 Ⓜ. `pip install email-validator`
⚖️ `pip install pydantic[email]`.

& 👥 ⚙️ 👉 🏷 📣 👆 🔢 & 🎏 🏷 📣 👆 🔢:

=== "🐍 3️⃣.6️⃣ & 🔛"

```Python hl_lines="18"
{!> ../../../docs_src/response_model/tutorial002.py!}
```

=== "🐍 3️⃣.1️⃣0️⃣ & 🔛"

```Python hl_lines="16"
{!> ../../../docs_src/response_model/tutorial002_py310.py!}
```

🔜, 🕐❔ 🖥 🏗 👩‍💻 ⏮️ 🔐, 🛠️ 🔜 📨 🎏 🔐 📨.

👉 💼, ⚫️ 💪 🚫 ⚠, ↩️ ⚫️ 🎏 👩‍💻 📨 🔐.

✋️ 🚥 👥 ⚙️ 🎏 🏷 ➕1️⃣ ➡ 🛠️, 👥 💪 📨 👆 👩‍💻 🔐 🔠 👩‍💻.

!!! danger 🙅 🏪 ✅ 🔐 👩‍💻 ⚖️ 📨 ⚫️ 📨 💖 👉, 🚥 👆 💭 🌐 ⚠ & 👆 💭 ⚫️❔ 👆 🔨.

🚮 🔢 🏷

👥 💪 ↩️ ✍ 🔢 🏷 ⏮️ 🔢 🔐 & 🔢 🏷 🍵 ⚫️:

=== "🐍 3️⃣.6️⃣ & 🔛"

```Python hl_lines="9  11  16"
{!> ../../../docs_src/response_model/tutorial003.py!}
```

=== "🐍 3️⃣.1️⃣0️⃣ & 🔛"

```Python hl_lines="9  11  16"
{!> ../../../docs_src/response_model/tutorial003_py310.py!}
```

📥, ✋️ 👆 ➡ 🛠️ 🔢 🛬 🎏 🔢 👩‍💻 👈 🔌 🔐:

=== "🐍 3️⃣.6️⃣ & 🔛"

```Python hl_lines="24"
{!> ../../../docs_src/response_model/tutorial003.py!}
```

=== "🐍 3️⃣.1️⃣0️⃣ & 🔛"

```Python hl_lines="24"
{!> ../../../docs_src/response_model/tutorial003_py310.py!}
```

...👥 📣 response_model 👆 🏷 UserOut, 👈 🚫 🔌 🔐:

=== "🐍 3️⃣.6️⃣ & 🔛"

```Python hl_lines="22"
{!> ../../../docs_src/response_model/tutorial003.py!}
```

=== "🐍 3️⃣.1️⃣0️⃣ & 🔛"

```Python hl_lines="22"
{!> ../../../docs_src/response_model/tutorial003_py310.py!}
```

, FastAPI 🔜 ✊ 💅 🖥 👅 🌐 💽 👈 🚫 📣 🔢 🏷 (⚙️ Pydantic).

response_model ⚖️ 📨 🆎

👉 💼, ↩️ 2️⃣ 🏷 🎏, 🚥 👥 ✍ 🔢 📨 🆎 UserOut, 👨‍🎨 & 🧰 🔜 😭 👈 👥 🛬 ❌ 🆎, 📚 🎏 🎓.

👈 ⚫️❔ 👉 🖼 👥 ✔️ 📣 ⚫️ response_model 🔢.

...✋️ 😣 👂 🔛 👀 ❔ ❎ 👈.

📨 🆎 & 💽 🖥

➡️ 😣 ⚪️➡️ ⏮️ 🖼. 👥 💚 ✍ 🔢 ⏮️ 1️⃣ 🆎 ✋️ 📨 🕳 👈 🔌 🌅 💽.

👥 💚 FastAPI 🚧 🖥 📊 ⚙️ 📨 🏷.

⏮️ 🖼, ↩️ 🎓 🎏, 👥 ✔️ ⚙️ response_model 🔢. ✋️ 👈 ⛓ 👈 👥 🚫 🤚 🐕‍🦺 ⚪️➡️ 👨‍🎨 & 🧰 ✅ 🔢 📨 🆎.

✋️ 🌅 💼 🌐❔ 👥 💪 🕳 💖 👉, 👥 💚 🏷 ⛽/❎ 📊 👉 🖼.

& 👈 💼, 👥 💪 ⚙️ 🎓 & 🧬 ✊ 📈 🔢 🆎 ✍ 🤚 👍 🐕‍🦺 👨‍🎨 & 🧰, & 🤚 FastAPI 💽 🖥.

=== "🐍 3️⃣.6️⃣ & 🔛"

```Python hl_lines="9-13  15-16  20"
{!> ../../../docs_src/response_model/tutorial003_01.py!}
```

=== "🐍 3️⃣.1️⃣0️⃣ & 🔛"

```Python hl_lines="7-10  13-14  18"
{!> ../../../docs_src/response_model/tutorial003_01_py310.py!}
```

⏮️ 👉, 👥 🤚 🏭 🐕‍🦺, ⚪️➡️ 👨‍🎨 & ✍ 👉 📟 ☑ ⚖ 🆎, ✋️ 👥 🤚 💽 🖥 ⚪️➡️ FastAPI.

❔ 🔨 👉 👷 ❓ ➡️ ✅ 👈 👅. 👶

🆎 ✍ & 🏭

🥇 ➡️ 👀 ❔ 👨‍🎨, ✍ & 🎏 🧰 🔜 👀 👉.

BaseUser ✔️ 🧢 🏑. ⤴️ UserIn 😖 ⚪️➡️ BaseUser & 🚮 password 🏑,, ⚫️ 🔜 🔌 🌐 🏑 ⚪️➡️ 👯‍♂️ 🏷.

👥 ✍ 🔢 📨 🆎 BaseUser, ✋️ 👥 🤙 🛬 UserIn 👐.

👨‍🎨, ✍, & 🎏 🧰 🏆 🚫 😭 🔃 👉 ↩️, ⌨ ⚖, UserIn 🏿 BaseUser, ❔ ⛓ ⚫️ 🆎 🕐❔ ⚫️❔ ⌛ 🕳 👈 BaseUser.

FastAPI 💽 🖥

🔜, FastAPI, ⚫️ 🔜 👀 📨 🆎 & ⚒ 💭 👈 ⚫️❔ 👆 📨 🔌 🕴 🏑 👈 📣 🆎.

FastAPI 🔨 📚 👜 🔘 ⏮️ Pydantic ⚒ 💭 👈 📚 🎏 🚫 🎓 🧬 🚫 ⚙️ 📨 💽 🖥, ⏪ 👆 💪 🔚 🆙 🛬 🌅 🌅 💽 🌘 ⚫️❔ 👆 📈.

👉 🌌, 👆 💪 🤚 🏆 👯‍♂️ 🌏: 🆎 ✍ ⏮️ 🏭 🐕‍🦺 & 💽 🖥.

👀 ⚫️ 🩺

🕐❔ 👆 👀 🏧 🩺, 👆 💪 ✅ 👈 🔢 🏷 & 🔢 🏷 🔜 👯‍♂️ ✔️ 👫 👍 🎻 🔗:

& 👯‍♂️ 🏷 🔜 ⚙️ 🎓 🛠️ 🧾:

🎏 📨 🆎 ✍

📤 5️⃣📆 💼 🌐❔ 👆 📨 🕳 👈 🚫 ☑ Pydantic 🏑 & 👆 ✍ ⚫️ 🔢, 🕴 🤚 🐕‍🦺 🚚 🏭 (👨‍🎨, ✍, ♒️).

📨 📨 🔗

🏆 ⚠ 💼 🔜 🛬 📨 🔗 🔬 ⏪ 🏧 🩺{.internal-link target=_blank}.

{!> ../../../docs_src/response_model/tutorial003_02.py!}

👉 🙅 💼 🍵 🔁 FastAPI ↩️ 📨 🆎 ✍ 🎓 (⚖️ 🏿) Response.

& 🧰 🔜 😄 ↩️ 👯‍♂️ RedirectResponse & JSONResponse 🏿 Response, 🆎 ✍ ☑.

✍ 📨 🏿

👆 💪 ⚙️ 🏿 Response 🆎 ✍:

{!> ../../../docs_src/response_model/tutorial003_03.py!}

👉 🔜 👷 ↩️ RedirectResponse 🏿 Response, & FastAPI 🔜 🔁 🍵 👉 🙅 💼.

❌ 📨 🆎 ✍

✋️ 🕐❔ 👆 📨 🎏 ❌ 🎚 👈 🚫 ☑ Pydantic 🆎 (✅ 💽 🎚) & 👆 ✍ ⚫️ 💖 👈 🔢, FastAPI 🔜 🔄 ✍ Pydantic 📨 🏷 ⚪️➡️ 👈 🆎 ✍, & 🔜 ❌.

🎏 🔜 🔨 🚥 👆 ✔️ 🕳 💖 🇪🇺 🖖 🎏 🆎 🌐❔ 1️⃣ ⚖️ 🌅 👫 🚫 ☑ Pydantic 🆎, 🖼 👉 🔜 ❌ 👶:

=== "🐍 3️⃣.6️⃣ & 🔛"

```Python hl_lines="10"
{!> ../../../docs_src/response_model/tutorial003_04.py!}
```

=== "🐍 3️⃣.1️⃣0️⃣ & 🔛"

```Python hl_lines="8"
{!> ../../../docs_src/response_model/tutorial003_04_py310.py!}
```

...👉 ❌ ↩️ 🆎 ✍ 🚫 Pydantic 🆎 & 🚫 👁 Response 🎓 ⚖️ 🏿, ⚫️ 🇪🇺 (🙆 2️⃣) 🖖 Response & dict.

❎ 📨 🏷

▶️ ⚪️➡️ 🖼 🔛, 👆 5️⃣📆 🚫 💚 ✔️ 🔢 💽 🔬, 🧾, 🖥, ♒️. 👈 🎭 FastAPI.

✋️ 👆 💪 💚 🚧 📨 🆎 ✍ 🔢 🤚 🐕‍🦺 ⚪️➡️ 🧰 💖 👨‍🎨 & 🆎 ☑ (✅ ✍).

👉 💼, 👆 💪 ❎ 📨 🏷 ⚡ ⚒ response_model=None:

=== "🐍 3️⃣.6️⃣ & 🔛"

```Python hl_lines="9"
{!> ../../../docs_src/response_model/tutorial003_05.py!}
```

=== "🐍 3️⃣.1️⃣0️⃣ & 🔛"

```Python hl_lines="7"
{!> ../../../docs_src/response_model/tutorial003_05_py310.py!}
```

👉 🔜 ⚒ FastAPI 🚶 📨 🏷 ⚡ & 👈 🌌 👆 💪 ✔️ 🙆 📨 🆎 ✍ 👆 💪 🍵 ⚫️ 🤕 👆 FastAPI 🈸. 👶

📨 🏷 🔢 🔢

👆 📨 🏷 💪 ✔️ 🔢 💲, 💖:

=== "🐍 3️⃣.6️⃣ & 🔛"

```Python hl_lines="11  13-14"
{!> ../../../docs_src/response_model/tutorial004.py!}
```

=== "🐍 3️⃣.9️⃣ & 🔛"

```Python hl_lines="11  13-14"
{!> ../../../docs_src/response_model/tutorial004_py39.py!}
```

=== "🐍 3️⃣.1️⃣0️⃣ & 🔛"

```Python hl_lines="9  11-12"
{!> ../../../docs_src/response_model/tutorial004_py310.py!}
```
  • description: Union[str, None] = None (⚖️ str | None = None 🐍 3️⃣.1️⃣0️⃣) ✔️ 🔢 None.
  • tax: float = 10.5 ✔️ 🔢 10.5.
  • tags: List[str] = [] 🔢 🛁 📇: [].

✋️ 👆 💪 💚 🚫 👫 ⚪️➡️ 🏁 🚥 👫 🚫 🤙 🏪.

🖼, 🚥 👆 ✔️ 🏷 ⏮️ 📚 📦 🔢 ☁ 💽, ✋️ 👆 🚫 💚 📨 📶 📏 🎻 📨 🌕 🔢 💲.

⚙️ response_model_exclude_unset 🔢

👆 💪 ⚒ ➡ 🛠️ 👨‍🎨 🔢 response_model_exclude_unset=True:

=== "🐍 3️⃣.6️⃣ & 🔛"

```Python hl_lines="24"
{!> ../../../docs_src/response_model/tutorial004.py!}
```

=== "🐍 3️⃣.9️⃣ & 🔛"

```Python hl_lines="24"
{!> ../../../docs_src/response_model/tutorial004_py39.py!}
```

=== "🐍 3️⃣.1️⃣0️⃣ & 🔛"

```Python hl_lines="22"
{!> ../../../docs_src/response_model/tutorial004_py310.py!}
```

& 👈 🔢 💲 🏆 🚫 🔌 📨, 🕴 💲 🤙 ⚒.

, 🚥 👆 📨 📨 👈 ➡ 🛠️ 🏬 ⏮️ 🆔 foo, 📨 (🚫 ✅ 🔢 💲) 🔜:

{
    "name": "Foo",
    "price": 50.2
}

!!! info FastAPI ⚙️ Pydantic 🏷 .dict() ⏮️ 🚮 exclude_unset 🔢 🏆 👉.

!!! info 👆 💪 ⚙️:

* `response_model_exclude_defaults=True`
* `response_model_exclude_none=True`

🔬 <a href="https://docs.pydantic.dev/latest/concepts/serialization/#modeldict" class="external-link" target="_blank">Pydantic 🩺</a> `exclude_defaults` &amp; `exclude_none`.

📊 ⏮️ 💲 🏑 ⏮️ 🔢

✋️ 🚥 👆 📊 ✔️ 💲 🏷 🏑 ⏮️ 🔢 💲, 💖 🏬 ⏮️ 🆔 bar:

{
    "name": "Bar",
    "description": "The bartenders",
    "price": 62,
    "tax": 20.2
}

👫 🔜 🔌 📨.

📊 ⏮️ 🎏 💲 🔢

🚥 📊 ✔️ 🎏 💲 🔢 🕐, 💖 🏬 ⏮️ 🆔 baz:

{
    "name": "Baz",
    "description": None,
    "price": 50.2,
    "tax": 10.5,
    "tags": []
}

FastAPI 🙃 🥃 (🤙, Pydantic 🙃 🥃) 🤔 👈, ✋️ description, tax, & tags ✔️ 🎏 💲 🔢, 👫 ⚒ 🎯 (↩️ ✊ ⚪️➡️ 🔢).

, 👫 🔜 🔌 🎻 📨.

!!! tip 👀 👈 🔢 💲 💪 🕳, 🚫 🕴 None.

👫 💪 📇 (`[]`), `float` `10.5`, ♒️.

response_model_include & response_model_exclude

👆 💪 ⚙️ ➡ 🛠️ 👨‍🎨 🔢 response_model_include & response_model_exclude.

👫 ✊ set str ⏮️ 📛 🔢 🔌 (❎ 🎂) ⚖️ 🚫 (✅ 🎂).

👉 💪 ⚙️ ⏩ ⌨ 🚥 👆 ✔️ 🕴 1️⃣ Pydantic 🏷 & 💚 ❎ 💽 ⚪️➡️ 🔢.

!!! tip ✋️ ⚫️ 👍 ⚙️ 💭 🔛, ⚙️ 💗 🎓, ↩️ 👫 🔢.

👉 ↩️ 🎻 🔗 🏗 👆 📱 🗄 (&amp; 🩺) 🔜 1️⃣ 🏁 🏷, 🚥 👆 ⚙️ `response_model_include` ⚖️ `response_model_exclude` 🚫 🔢.

👉 ✔ `response_model_by_alias` 👈 👷 ➡.

=== "🐍 3️⃣.6️⃣ & 🔛"

```Python hl_lines="31  37"
{!> ../../../docs_src/response_model/tutorial005.py!}
```

=== "🐍 3️⃣.1️⃣0️⃣ & 🔛"

```Python hl_lines="29  35"
{!> ../../../docs_src/response_model/tutorial005_py310.py!}
```

!!! tip ❕ {"name", "description"}set ⏮️ 📚 2️⃣ 💲.

⚫️ 🌓 `set(["name", "description"])`.

⚙️ listⓂ ↩️ set

🚥 👆 💭 ⚙️ set & ⚙️ list ⚖️ tuple ↩️, FastAPI 🔜 🗜 ⚫️ set & ⚫️ 🔜 👷 ☑:

=== "🐍 3️⃣.6️⃣ & 🔛"

```Python hl_lines="31  37"
{!> ../../../docs_src/response_model/tutorial006.py!}
```

=== "🐍 3️⃣.1️⃣0️⃣ & 🔛"

```Python hl_lines="29  35"
{!> ../../../docs_src/response_model/tutorial006_py310.py!}
```

🌃

⚙️ ➡ 🛠️ 👨‍🎨 🔢 response_model 🔬 📨 🏷 & ✴️ 🚚 📢 💽 ⛽ 👅.

⚙️ response_model_exclude_unset 📨 🕴 💲 🎯 ⚒.