🌐 🔠 📄 🐢 Translate docs to Emoji 🥳 🎉 💥 🤯 🤯 (#5385)

* 🌐 💬  🩺  🦲

* 🎨 [pre-commit.ci] Auto format from pre-commit.com hooks

* 🛠️😊

* ♻️ Rename emoji lang from emj to em, and main docs name as 😉

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Axd1x8a <26704473+FeeeeK@users.noreply.github.com>
Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
This commit is contained in:
LeeeeT
2023-04-01 12:26:04 +03:00
committed by GitHub
parent d666ccb622
commit d4e85da18b
125 changed files with 18865 additions and 2 deletions

View File

@@ -0,0 +1,102 @@
# 🖥 📋
👆 💪 🔬 🖥 📋 🏃 *⏮️* 🛬 📨.
👉 ⚠ 🛠️ 👈 💪 🔨 ⏮️ 📨, ✋️ 👈 👩‍💻 🚫 🤙 ✔️ ⌛ 🛠️ 🏁 ⏭ 📨 📨.
👉 🔌, 🖼:
* 📧 📨 📨 ⏮️ 🎭 🎯:
* 🔗 📧 💽 &amp; 📨 📧 😑 "🐌" (📚 🥈), 👆 💪 📨 📨 ▶️️ ↖️ &amp; 📨 📧 📨 🖥.
* 🏭 💽:
* 🖼, ➡️ 💬 👆 📨 📁 👈 🔜 🚶 🔘 🐌 🛠️, 👆 💪 📨 📨 "🚫" (🇺🇸🔍 2⃣0⃣2⃣) &amp; 🛠️ ⚫️ 🖥.
## ⚙️ `BackgroundTasks`
🥇, 🗄 `BackgroundTasks` &amp; 🔬 🔢 👆 *➡ 🛠️ 🔢* ⏮️ 🆎 📄 `BackgroundTasks`:
```Python hl_lines="1 13"
{!../../../docs_src/background_tasks/tutorial001.py!}
```
**FastAPI** 🔜 ✍ 🎚 🆎 `BackgroundTasks` 👆 &amp; 🚶‍♀️ ⚫️ 👈 🔢.
## ✍ 📋 🔢
✍ 🔢 🏃 🖥 📋.
⚫️ 🐩 🔢 👈 💪 📨 🔢.
⚫️ 💪 `async def` ⚖️ 😐 `def` 🔢, **FastAPI** 🔜 💭 ❔ 🍵 ⚫️ ☑.
👉 💼, 📋 🔢 🔜 ✍ 📁 (⚖ 📨 📧).
&amp; ✍ 🛠️ 🚫 ⚙️ `async` &amp; `await`, 👥 🔬 🔢 ⏮️ 😐 `def`:
```Python hl_lines="6-9"
{!../../../docs_src/background_tasks/tutorial001.py!}
```
## 🚮 🖥 📋
🔘 👆 *➡ 🛠️ 🔢*, 🚶‍♀️ 👆 📋 🔢 *🖥 📋* 🎚 ⏮️ 👩‍🔬 `.add_task()`:
```Python hl_lines="14"
{!../../../docs_src/background_tasks/tutorial001.py!}
```
`.add_task()` 📨 ❌:
* 📋 🔢 🏃 🖥 (`write_notification`).
* 🙆 🔁 ❌ 👈 🔜 🚶‍♀️ 📋 🔢 ✔ (`email`).
* 🙆 🇨🇻 ❌ 👈 🔜 🚶‍♀️ 📋 🔢 (`message="some notification"`).
## 🔗 💉
⚙️ `BackgroundTasks` 👷 ⏮️ 🔗 💉 ⚙️, 👆 💪 📣 🔢 🆎 `BackgroundTasks` 💗 🎚: *➡ 🛠️ 🔢*, 🔗 (☑), 🎧-🔗, ♒️.
**FastAPI** 💭 ⚫️❔ 🔠 💼 &amp; ❔ 🏤-⚙️ 🎏 🎚, 👈 🌐 🖥 📋 🔗 👯‍♂️ &amp; 🏃 🖥 ⏮️:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="13 15 22 25"
{!> ../../../docs_src/background_tasks/tutorial002.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="11 13 20 23"
{!> ../../../docs_src/background_tasks/tutorial002_py310.py!}
```
👉 🖼, 📧 🔜 ✍ `log.txt` 📁 *⏮️* 📨 📨.
🚥 📤 🔢 📨, ⚫️ 🔜 ✍ 🕹 🖥 📋.
&amp; ⤴️ 1⃣ 🖥 📋 🏗 *➡ 🛠️ 🔢* 🔜 ✍ 📧 ⚙️ `email` ➡ 🔢.
## 📡
🎓 `BackgroundTasks` 👟 🔗 ⚪️➡️ <a href="https://www.starlette.io/background/" class="external-link" target="_blank">`starlette.background`</a>.
⚫️ 🗄/🔌 🔗 🔘 FastAPI 👈 👆 💪 🗄 ⚫️ ⚪️➡️ `fastapi` &amp; ❎ 😫 🗄 🎛 `BackgroundTask` (🍵 `s` 🔚) ⚪️➡️ `starlette.background`.
🕴 ⚙️ `BackgroundTasks` (&amp; 🚫 `BackgroundTask`), ⚫️ ⤴️ 💪 ⚙️ ⚫️ *➡ 🛠️ 🔢* 🔢 &amp; ✔️ **FastAPI** 🍵 🎂 👆, 💖 🕐❔ ⚙️ `Request` 🎚 🔗.
⚫️ 💪 ⚙️ `BackgroundTask` 😞 FastAPI, ✋️ 👆 ✔️ ✍ 🎚 👆 📟 &amp; 📨 💃 `Response` 🔌 ⚫️.
👆 💪 👀 🌖 <a href="https://www.starlette.io/background/" class="external-link" target="_blank">💃 🛂 🩺 🖥 📋</a>.
## ⚠
🚥 👆 💪 🎭 🏋️ 🖥 📊 &amp; 👆 🚫 🎯 💪 ⚫️ 🏃 🎏 🛠️ (🖼, 👆 🚫 💪 💰 💾, 🔢, ♒️), 👆 💪 💰 ⚪️➡️ ⚙️ 🎏 🦏 🧰 💖 <a href="https://docs.celeryq.dev" class="external-link" target="_blank">🥒</a>.
👫 😑 🚚 🌖 🏗 📳, 📧/👨‍🏭 📤 👨‍💼, 💖 ✳ ⚖️ ✳, ✋️ 👫 ✔ 👆 🏃 🖥 📋 💗 🛠️, &amp; ✴️, 💗 💽.
👀 🖼, ✅ [🏗 🚂](../project-generation.md){.internal-link target=_blank}, 👫 🌐 🔌 🥒 ⏪ 📶.
✋️ 🚥 👆 💪 🔐 🔢 &amp; 🎚 ⚪️➡️ 🎏 **FastAPI** 📱, ⚖️ 👆 💪 🎭 🤪 🖥 📋 (💖 📨 📧 📨), 👆 💪 🎯 ⚙️ `BackgroundTasks`.
## 🌃
🗄 &amp; ⚙️ `BackgroundTasks` ⏮️ 🔢 *➡ 🛠️ 🔢* &amp; 🔗 🚮 🖥 📋.

View File

@@ -0,0 +1,488 @@
# 🦏 🈸 - 💗 📁
🚥 👆 🏗 🈸 ⚖️ 🕸 🛠️, ⚫️ 🛎 💼 👈 👆 💪 🚮 🌐 🔛 👁 📁.
**FastAPI** 🚚 🏪 🧰 📊 👆 🈸 ⏪ 🚧 🌐 💪.
!!! info
🚥 👆 👟 ⚪️➡️ 🏺, 👉 🔜 🌓 🏺 📗.
## 🖼 📁 📊
➡️ 💬 👆 ✔️ 📁 📊 💖 👉:
```
.
├── app
│   ├── __init__.py
│   ├── main.py
│   ├── dependencies.py
│   └── routers
│   │ ├── __init__.py
│   │ ├── items.py
│   │ └── users.py
│   └── internal
│   ├── __init__.py
│   └── admin.py
```
!!! tip
📤 📚 `__init__.py` 📁: 1⃣ 🔠 📁 ⚖️ 📁.
👉 ⚫️❔ ✔ 🏭 📟 ⚪️➡️ 1⃣ 📁 🔘 1⃣.
🖼, `app/main.py` 👆 💪 ✔️ ⏸ 💖:
```
from app.routers import items
```
* `app` 📁 🔌 🌐. &amp; ⚫️ ✔️ 🛁 📁 `app/__init__.py`, ⚫️ "🐍 📦" (🗃 "🐍 🕹"): `app`.
* ⚫️ 🔌 `app/main.py` 📁. ⚫️ 🔘 🐍 📦 (📁 ⏮️ 📁 `__init__.py`), ⚫️ "🕹" 👈 📦: `app.main`.
* 📤 `app/dependencies.py` 📁, 💖 `app/main.py`, ⚫️ "🕹": `app.dependencies`.
* 📤 📁 `app/routers/` ⏮️ 1⃣ 📁 `__init__.py`, ⚫️ "🐍 📦": `app.routers`.
* 📁 `app/routers/items.py` 🔘 📦, `app/routers/`,, ⚫️ 🔁: `app.routers.items`.
* 🎏 ⏮️ `app/routers/users.py`, ⚫️ 1⃣ 🔁: `app.routers.users`.
* 📤 📁 `app/internal/` ⏮️ 1⃣ 📁 `__init__.py`, ⚫️ 1⃣ "🐍 📦": `app.internal`.
* &amp; 📁 `app/internal/admin.py` 1⃣ 🔁: `app.internal.admin`.
<img src="/img/tutorial/bigger-applications/package.svg">
🎏 📁 📊 ⏮️ 🏤:
```
.
├── app # "app" is a Python package
│   ├── __init__.py # this file makes "app" a "Python package"
│   ├── main.py # "main" module, e.g. import app.main
│   ├── dependencies.py # "dependencies" module, e.g. import app.dependencies
│   └── routers # "routers" is a "Python subpackage"
│   │ ├── __init__.py # makes "routers" a "Python subpackage"
│   │ ├── items.py # "items" submodule, e.g. import app.routers.items
│   │ └── users.py # "users" submodule, e.g. import app.routers.users
│   └── internal # "internal" is a "Python subpackage"
│   ├── __init__.py # makes "internal" a "Python subpackage"
│   └── admin.py # "admin" submodule, e.g. import app.internal.admin
```
## `APIRouter`
➡️ 💬 📁 💡 🚚 👩‍💻 🔁 `/app/routers/users.py`.
👆 💚 ✔️ *➡ 🛠️* 🔗 👆 👩‍💻 👽 ⚪️➡️ 🎂 📟, 🚧 ⚫️ 🏗.
✋️ ⚫️ 🍕 🎏 **FastAPI** 🈸/🕸 🛠️ (⚫️ 🍕 🎏 "🐍 📦").
👆 💪 ✍ *➡ 🛠️* 👈 🕹 ⚙️ `APIRouter`.
### 🗄 `APIRouter`
👆 🗄 ⚫️ &amp; ✍ "👐" 🎏 🌌 👆 🔜 ⏮️ 🎓 `FastAPI`:
```Python hl_lines="1 3"
{!../../../docs_src/bigger_applications/app/routers/users.py!}
```
### *➡ 🛠️* ⏮️ `APIRouter`
&amp; ⤴️ 👆 ⚙️ ⚫️ 📣 👆 *➡ 🛠️*.
⚙️ ⚫️ 🎏 🌌 👆 🔜 ⚙️ `FastAPI` 🎓:
```Python hl_lines="6 11 16"
{!../../../docs_src/bigger_applications/app/routers/users.py!}
```
👆 💪 💭 `APIRouter` "🐩 `FastAPI`" 🎓.
🌐 🎏 🎛 🐕‍🦺.
🌐 🎏 `parameters`, `responses`, `dependencies`, `tags`, ♒️.
!!! tip
👉 🖼, 🔢 🤙 `router`, ✋️ 👆 💪 📛 ⚫️ 👐 👆 💚.
👥 🔜 🔌 👉 `APIRouter` 👑 `FastAPI` 📱, ✋️ 🥇, ➡️ ✅ 🔗 &amp; 1⃣ `APIRouter`.
## 🔗
👥 👀 👈 👥 🔜 💪 🔗 ⚙️ 📚 🥉 🈸.
👥 🚮 👫 👫 👍 `dependencies` 🕹 (`app/dependencies.py`).
👥 🔜 🔜 ⚙️ 🙅 🔗 ✍ 🛃 `X-Token` 🎚:
```Python hl_lines="1 4-6"
{!../../../docs_src/bigger_applications/app/dependencies.py!}
```
!!! tip
👥 ⚙️ 💭 🎚 📉 👉 🖼.
✋️ 🎰 💼 👆 🔜 🤚 👍 🏁 ⚙️ 🛠️ [💂‍♂ 🚙](./security/index.md){.internal-link target=_blank}.
## 1⃣ 🕹 ⏮️ `APIRouter`
➡️ 💬 👆 ✔️ 🔗 💡 🚚 "🏬" ⚪️➡️ 👆 🈸 🕹 `app/routers/items.py`.
👆 ✔️ *➡ 🛠️* :
* `/items/`
* `/items/{item_id}`
⚫️ 🌐 🎏 📊 ⏮️ `app/routers/users.py`.
✋️ 👥 💚 🙃 &amp; 📉 📟 🍖.
👥 💭 🌐 *➡ 🛠️* 👉 🕹 ✔️ 🎏:
* ➡ `prefix`: `/items`.
* `tags`: (1⃣ 🔖: `items`).
* `responses`.
* `dependencies`: 👫 🌐 💪 👈 `X-Token` 🔗 👥 ✍.
, ↩️ ❎ 🌐 👈 🔠 *➡ 🛠️*, 👥 💪 🚮 ⚫️ `APIRouter`.
```Python hl_lines="5-10 16 21"
{!../../../docs_src/bigger_applications/app/routers/items.py!}
```
➡ 🔠 *➡ 🛠️* ✔️ ▶️ ⏮️ `/`, 💖:
```Python hl_lines="1"
@router.get("/{item_id}")
async def read_item(item_id: str):
...
```
...🔡 🔜 🚫 🔌 🏁 `/`.
, 🔡 👉 💼 `/items`.
👥 💪 🚮 📇 `tags` &amp; `responses` 👈 🔜 ✔ 🌐 *➡ 🛠️* 🔌 👉 📻.
&amp; 👥 💪 🚮 📇 `dependencies` 👈 🔜 🚮 🌐 *➡ 🛠️* 📻 &amp; 🔜 🛠️/❎ 🔠 📨 ⚒ 👫.
!!! tip
🗒 👈, 🌅 💖 [🔗 *➡ 🛠️ 👨‍🎨*](dependencies/dependencies-in-path-operation-decorators.md){.internal-link target=_blank}, 🙅‍♂ 💲 🔜 🚶‍♀️ 👆 *➡ 🛠️ 🔢*.
🔚 🏁 👈 🏬 ➡ 🔜:
* `/items/`
* `/items/{item_id}`
...👥 🎯.
* 👫 🔜 ™ ⏮️ 📇 🔖 👈 🔌 👁 🎻 `"items"`.
* 👫 "🔖" ✴️ ⚠ 🏧 🎓 🧾 ⚙️ (⚙️ 🗄).
* 🌐 👫 🔜 🔌 🔁 `responses`.
* 🌐 👫 *➡ 🛠️* 🔜 ✔️ 📇 `dependencies` 🔬/🛠️ ⏭ 👫.
* 🚥 👆 📣 🔗 🎯 *➡ 🛠️*, **👫 🔜 🛠️ 💁‍♂️**.
* 📻 🔗 🛠️ 🥇, ⤴️ [`dependencies` 👨‍🎨](dependencies/dependencies-in-path-operation-decorators.md){.internal-link target=_blank}, &amp; ⤴️ 😐 🔢 🔗.
* 👆 💪 🚮 [`Security` 🔗 ⏮️ `scopes`](../advanced/security/oauth2-scopes.md){.internal-link target=_blank}.
!!! tip
✔️ `dependencies` `APIRouter` 💪 ⚙️, 🖼, 🚚 🤝 🎂 👪 *➡ 🛠️*. 🚥 🔗 🚫 🚮 📦 🔠 1⃣ 👫.
!!! check
`prefix`, `tags`, `responses`, &amp; `dependencies` 🔢 (📚 🎏 💼) ⚒ ⚪️➡️ **FastAPI** 👆 ❎ 📟 ❎.
### 🗄 🔗
👉 📟 👨‍❤‍👨 🕹 `app.routers.items`, 📁 `app/routers/items.py`.
&amp; 👥 💪 🤚 🔗 🔢 ⚪️➡️ 🕹 `app.dependencies`, 📁 `app/dependencies.py`.
👥 ⚙️ ⚖ 🗄 ⏮️ `..` 🔗:
```Python hl_lines="3"
{!../../../docs_src/bigger_applications/app/routers/items.py!}
```
#### ❔ ⚖ 🗄 👷
!!! tip
🚥 👆 💭 👌 ❔ 🗄 👷, 😣 ⏭ 📄 🔛.
👁 ❣ `.`, 💖:
```Python
from .dependencies import get_token_header
```
🔜 ⛓:
* ▶️ 🎏 📦 👈 👉 🕹 (📁 `app/routers/items.py`) 🖖 (📁 `app/routers/`)...
* 🔎 🕹 `dependencies` (👽 📁 `app/routers/dependencies.py`)...
* &amp; ⚪️➡️ ⚫️, 🗄 🔢 `get_token_header`.
✋️ 👈 📁 🚫 🔀, 👆 🔗 📁 `app/dependencies.py`.
💭 ❔ 👆 📱/📁 📊 👀 💖:
<img src="/img/tutorial/bigger-applications/package.svg">
---
2⃣ ❣ `..`, 💖:
```Python
from ..dependencies import get_token_header
```
⛓:
* ▶️ 🎏 📦 👈 👉 🕹 (📁 `app/routers/items.py`) 🖖 (📁 `app/routers/`)...
* 🚶 👪 📦 (📁 `app/`)...
* &amp; 📤, 🔎 🕹 `dependencies` (📁 `app/dependencies.py`)...
* &amp; ⚪️➡️ ⚫️, 🗄 🔢 `get_token_header`.
👈 👷 ☑ ❗ 👶
---
🎏 🌌, 🚥 👥 ✔️ ⚙️ 3⃣ ❣ `...`, 💖:
```Python
from ...dependencies import get_token_header
```
that 🔜 ⛓:
* ▶️ 🎏 📦 👈 👉 🕹 (📁 `app/routers/items.py`) 🖖 (📁 `app/routers/`)...
* 🚶 👪 📦 (📁 `app/`)...
* ⤴️ 🚶 👪 👈 📦 (📤 🙅‍♂ 👪 📦, `app` 🔝 🎚 👶)...
* &amp; 📤, 🔎 🕹 `dependencies` (📁 `app/dependencies.py`)...
* &amp; ⚪️➡️ ⚫️, 🗄 🔢 `get_token_header`.
👈 🔜 🔗 📦 🔛 `app/`, ⏮️ 🚮 👍 📁 `__init__.py`, ♒️. ✋️ 👥 🚫 ✔️ 👈. , 👈 🔜 🚮 ❌ 👆 🖼. 👶
✋️ 🔜 👆 💭 ❔ ⚫️ 👷, 👆 💪 ⚙️ ⚖ 🗄 👆 👍 📱 🙅‍♂ 🤔 ❔ 🏗 👫. 👶
### 🚮 🛃 `tags`, `responses`, &amp; `dependencies`
👥 🚫 ❎ 🔡 `/items` 🚫 `tags=["items"]` 🔠 *➡ 🛠️* ↩️ 👥 🚮 👫 `APIRouter`.
✋️ 👥 💪 🚮 _🌅_ `tags` 👈 🔜 ✔ 🎯 *➡ 🛠️*, &amp; `responses` 🎯 👈 *➡ 🛠️*:
```Python hl_lines="30-31"
{!../../../docs_src/bigger_applications/app/routers/items.py!}
```
!!! tip
👉 🏁 ➡ 🛠️ 🔜 ✔️ 🌀 🔖: `["items", "custom"]`.
&amp; ⚫️ 🔜 ✔️ 👯‍♂️ 📨 🧾, 1⃣ `404` &amp; 1⃣ `403`.
## 👑 `FastAPI`
🔜, ➡️ 👀 🕹 `app/main.py`.
📥 🌐❔ 👆 🗄 &amp; ⚙️ 🎓 `FastAPI`.
👉 🔜 👑 📁 👆 🈸 👈 👔 🌐 👯‍♂️.
&amp; 🏆 👆 ⚛ 🔜 🔜 🖖 🚮 👍 🎯 🕹, 👑 📁 🔜 🙅.
### 🗄 `FastAPI`
👆 🗄 &amp; ✍ `FastAPI` 🎓 🛎.
&amp; 👥 💪 📣 [🌐 🔗](dependencies/global-dependencies.md){.internal-link target=_blank} 👈 🔜 🌀 ⏮️ 🔗 🔠 `APIRouter`:
```Python hl_lines="1 3 7"
{!../../../docs_src/bigger_applications/app/main.py!}
```
### 🗄 `APIRouter`
🔜 👥 🗄 🎏 🔁 👈 ✔️ `APIRouter`Ⓜ:
```Python hl_lines="5"
{!../../../docs_src/bigger_applications/app/main.py!}
```
📁 `app/routers/users.py` &amp; `app/routers/items.py` 🔁 👈 🍕 🎏 🐍 📦 `app`, 👥 💪 ⚙️ 👁 ❣ `.` 🗄 👫 ⚙️ "⚖ 🗄".
### ❔ 🏭 👷
📄:
```Python
from .routers import items, users
```
⛓:
* ▶️ 🎏 📦 👈 👉 🕹 (📁 `app/main.py`) 🖖 (📁 `app/`)...
* 👀 📦 `routers` (📁 `app/routers/`)...
* &amp; ⚪️➡️ ⚫️, 🗄 🔁 `items` (📁 `app/routers/items.py`) &amp; `users` (📁 `app/routers/users.py`)...
🕹 `items` 🔜 ✔️ 🔢 `router` (`items.router`). 👉 🎏 1⃣ 👥 ✍ 📁 `app/routers/items.py`, ⚫️ `APIRouter` 🎚.
&amp; ⤴️ 👥 🎏 🕹 `users`.
👥 💪 🗄 👫 💖:
```Python
from app.routers import items, users
```
!!! info
🥇 ⏬ "⚖ 🗄":
```Python
from .routers import items, users
```
🥈 ⏬ "🎆 🗄":
```Python
from app.routers import items, users
```
💡 🌅 🔃 🐍 📦 &amp; 🕹, ✍ <a href="https://docs.python.org/3/tutorial/modules.html" class="external-link" target="_blank">🛂 🐍 🧾 🔃 🕹</a>.
### ❎ 📛 💥
👥 🏭 🔁 `items` 🔗, ↩️ 🏭 🚮 🔢 `router`.
👉 ↩️ 👥 ✔️ 1⃣ 🔢 📛 `router` 🔁 `users`.
🚥 👥 ✔️ 🗄 1⃣ ⏮️ 🎏, 💖:
```Python
from .routers.items import router
from .routers.users import router
```
`router` ⚪️➡️ `users` 🔜 📁 1⃣ ⚪️➡️ `items` &amp; 👥 🚫🔜 💪 ⚙️ 👫 🎏 🕰.
, 💪 ⚙️ 👯‍♂️ 👫 🎏 📁, 👥 🗄 🔁 🔗:
```Python hl_lines="4"
{!../../../docs_src/bigger_applications/app/main.py!}
```
### 🔌 `APIRouter`Ⓜ `users` &amp; `items`
🔜, ➡️ 🔌 `router`Ⓜ ⚪️➡️ 🔁 `users` &amp; `items`:
```Python hl_lines="10-11"
{!../../../docs_src/bigger_applications/app/main.py!}
```
!!! info
`users.router` 🔌 `APIRouter` 🔘 📁 `app/routers/users.py`.
&amp; `items.router` 🔌 `APIRouter` 🔘 📁 `app/routers/items.py`.
⏮️ `app.include_router()` 👥 💪 🚮 🔠 `APIRouter` 👑 `FastAPI` 🈸.
⚫️ 🔜 🔌 🌐 🛣 ⚪️➡️ 👈 📻 🍕 ⚫️.
!!! note "📡 "
⚫️ 🔜 🤙 🔘 ✍ *➡ 🛠️* 🔠 *➡ 🛠️* 👈 📣 `APIRouter`.
, ⛅ 🎑, ⚫️ 🔜 🤙 👷 🚥 🌐 🎏 👁 📱.
!!! check
👆 🚫 ✔️ 😟 🔃 🎭 🕐❔ ✅ 📻.
👉 🔜 ✊ ⏲ &amp; 🔜 🕴 🔨 🕴.
⚫️ 🏆 🚫 📉 🎭. 👶
### 🔌 `APIRouter` ⏮️ 🛃 `prefix`, `tags`, `responses`, &amp; `dependencies`
🔜, ➡️ 🌈 👆 🏢 🤝 👆 `app/internal/admin.py` 📁.
⚫️ 🔌 `APIRouter` ⏮️ 📡 *➡ 🛠️* 👈 👆 🏢 💰 🖖 📚 🏗.
👉 🖼 ⚫️ 🔜 💎 🙅. ✋️ ➡️ 💬 👈 ↩️ ⚫️ 💰 ⏮️ 🎏 🏗 🏢, 👥 🚫🔜 🔀 ⚫️ &amp; 🚮 `prefix`, `dependencies`, `tags`, ♒️. 🔗 `APIRouter`:
```Python hl_lines="3"
{!../../../docs_src/bigger_applications/app/internal/admin.py!}
```
✋️ 👥 💚 ⚒ 🛃 `prefix` 🕐❔ ✅ `APIRouter` 👈 🌐 🚮 *➡ 🛠️* ▶️ ⏮️ `/admin`, 👥 💚 🔐 ⚫️ ⏮️ `dependencies` 👥 ⏪ ✔️ 👉 🏗, &amp; 👥 💚 🔌 `tags` &amp; `responses`.
👥 💪 📣 🌐 👈 🍵 ✔️ 🔀 ⏮️ `APIRouter` 🚶‍♀️ 👈 🔢 `app.include_router()`:
```Python hl_lines="14-17"
{!../../../docs_src/bigger_applications/app/main.py!}
```
👈 🌌, ⏮️ `APIRouter` 🔜 🚧 ⚗, 👥 💪 💰 👈 🎏 `app/internal/admin.py` 📁 ⏮️ 🎏 🏗 🏢.
🏁 👈 👆 📱, 🔠 *➡ 🛠️* ⚪️➡️ `admin` 🕹 🔜 ✔️:
* 🔡 `/admin`.
* 🔖 `admin`.
* 🔗 `get_token_header`.
* 📨 `418`. 👶
✋️ 👈 🔜 🕴 📉 👈 `APIRouter` 👆 📱, 🚫 🙆 🎏 📟 👈 ⚙️ ⚫️.
, 🖼, 🎏 🏗 💪 ⚙️ 🎏 `APIRouter` ⏮️ 🎏 🤝 👩‍🔬.
### 🔌 *➡ 🛠️*
👥 💪 🚮 *➡ 🛠️* 🔗 `FastAPI` 📱.
📥 👥 ⚫️... 🎦 👈 👥 💪 🤷:
```Python hl_lines="21-23"
{!../../../docs_src/bigger_applications/app/main.py!}
```
&amp; ⚫️ 🔜 👷 ☑, 👯‍♂️ ⏮️ 🌐 🎏 *➡ 🛠️* 🚮 ⏮️ `app.include_router()`.
!!! info "📶 📡 "
**🗒**: 👉 📶 📡 👈 👆 🎲 💪 **🚶**.
---
`APIRouter`Ⓜ 🚫 "🗻", 👫 🚫 👽 ⚪️➡️ 🎂 🈸.
👉 ↩️ 👥 💚 🔌 👫 *➡ 🛠️* 🗄 🔗 &amp; 👩‍💻 🔢.
👥 🚫🔜 ❎ 👫 &amp; "🗻" 👫 ➡ 🎂, *➡ 🛠️* "🖖" (🏤-✍), 🚫 🔌 🔗.
## ✅ 🏧 🛠️ 🩺
🔜, 🏃 `uvicorn`, ⚙️ 🕹 `app.main` &amp; 🔢 `app`:
<div class="termy">
```console
$ uvicorn app.main:app --reload
<span style="color: green;">INFO</span>: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
```
</div>
&amp; 📂 🩺 <a href="http://127.0.0.1:8000/docs" class="external-link" target="_blank">http://127.0.0.1:8000/docs</a>.
👆 🔜 👀 🏧 🛠️ 🩺, ✅ ➡ ⚪️➡️ 🌐 🔁, ⚙️ ☑ ➡ (&amp; 🔡) &amp; ☑ 🔖:
<img src="/img/tutorial/bigger-applications/image01.png">
## 🔌 🎏 📻 💗 🕰 ⏮️ 🎏 `prefix`
👆 💪 ⚙️ `.include_router()` 💗 🕰 ⏮️ *🎏* 📻 ⚙️ 🎏 🔡.
👉 💪 ⚠, 🖼, 🎦 🎏 🛠️ 🔽 🎏 🔡, ✅ `/api/v1` &amp; `/api/latest`.
👉 🏧 ⚙️ 👈 👆 5⃣📆 🚫 🤙 💪, ✋️ ⚫️ 📤 💼 👆.
## 🔌 `APIRouter` 1
🎏 🌌 👆 💪 🔌 `APIRouter` `FastAPI` 🈸, 👆 💪 🔌 `APIRouter` 1⃣ `APIRouter` ⚙️:
```Python
router.include_router(other_router)
```
⚒ 💭 👆 ⚫️ ⏭ 🔌 `router` `FastAPI` 📱, 👈 *➡ 🛠️* ⚪️➡️ `other_router` 🔌.

View File

@@ -0,0 +1,68 @@
# 💪 - 🏑
🎏 🌌 👆 💪 📣 🌖 🔬 &amp; 🗃 *➡ 🛠️ 🔢* 🔢 ⏮️ `Query`, `Path` &amp; `Body`, 👆 💪 📣 🔬 &amp; 🗃 🔘 Pydantic 🏷 ⚙️ Pydantic `Field`.
## 🗄 `Field`
🥇, 👆 ✔️ 🗄 ⚫️:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="4"
{!> ../../../docs_src/body_fields/tutorial001.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="2"
{!> ../../../docs_src/body_fields/tutorial001_py310.py!}
```
!!! warning
👀 👈 `Field` 🗄 🔗 ⚪️➡️ `pydantic`, 🚫 ⚪️➡️ `fastapi` 🌐 🎂 (`Query`, `Path`, `Body`, ♒️).
## 📣 🏷 🔢
👆 💪 ⤴️ ⚙️ `Field` ⏮️ 🏷 🔢:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="11-14"
{!> ../../../docs_src/body_fields/tutorial001.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="9-12"
{!> ../../../docs_src/body_fields/tutorial001_py310.py!}
```
`Field` 👷 🎏 🌌 `Query`, `Path` &amp; `Body`, ⚫️ ✔️ 🌐 🎏 🔢, ♒️.
!!! note "📡 "
🤙, `Query`, `Path` &amp; 🎏 👆 🔜 👀 ⏭ ✍ 🎚 🏿 ⚠ `Param` 🎓, ❔ ⚫️ 🏿 Pydantic `FieldInfo` 🎓.
&amp; Pydantic `Field` 📨 👐 `FieldInfo` 👍.
`Body` 📨 🎚 🏿 `FieldInfo` 🔗. &amp; 📤 🎏 👆 🔜 👀 ⏪ 👈 🏿 `Body` 🎓.
💭 👈 🕐❔ 👆 🗄 `Query`, `Path`, &amp; 🎏 ⚪️➡️ `fastapi`, 👈 🤙 🔢 👈 📨 🎁 🎓.
!!! tip
👀 ❔ 🔠 🏷 🔢 ⏮️ 🆎, 🔢 💲 &amp; `Field` ✔️ 🎏 📊 *➡ 🛠️ 🔢* 🔢, ⏮️ `Field` ↩️ `Path`, `Query` &amp; `Body`.
## 🚮
👆 💪 📣 `Field`, `Query`, `Body`, ♒️. &amp; ⚫️ 🔜 🔌 🏗 🎻 🔗.
👆 🔜 💡 🌅 🔃 ❎ ⏪ 🩺, 🕐❔ 🏫 📣 🖼.
!!! warning
🔑 🚶‍♀️ `Field` 🔜 🎁 📉 🗄 🔗 👆 🈸.
👫 🔑 5⃣📆 🚫 🎯 🍕 🗄 🔧, 🗄 🧰, 🖼 [🗄 💳](https://validator.swagger.io/), 5⃣📆 🚫 👷 ⏮️ 👆 🏗 🔗.
## 🌃
👆 💪 ⚙️ Pydantic `Field` 📣 🔬 &amp; 🗃 🏷 🔢.
👆 💪 ⚙️ 🇨🇻 ❌ 🚶‍♀️ 🌖 🎻 🔗 🗃.

View File

@@ -0,0 +1,213 @@
# 💪 - 💗 🔢
🔜 👈 👥 ✔️ 👀 ❔ ⚙️ `Path` &amp; `Query`, ➡️ 👀 🌅 🏧 ⚙️ 📨 💪 📄.
## 🌀 `Path`, `Query` &amp; 💪 🔢
🥇, ↗️, 👆 💪 🌀 `Path`, `Query` &amp; 📨 💪 🔢 📄 ➡ &amp; **FastAPI** 🔜 💭 ⚫️❔.
&amp; 👆 💪 📣 💪 🔢 📦, ⚒ 🔢 `None`:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="19-21"
{!> ../../../docs_src/body_multiple_params/tutorial001.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="17-19"
{!> ../../../docs_src/body_multiple_params/tutorial001_py310.py!}
```
!!! note
👀 👈, 👉 💼, `item` 👈 🔜 ✊ ⚪️➡️ 💪 📦. ⚫️ ✔️ `None` 🔢 💲.
## 💗 💪 🔢
⏮️ 🖼, *➡ 🛠️* 🔜 ⌛ 🎻 💪 ⏮️ 🔢 `Item`, 💖:
```JSON
{
"name": "Foo",
"description": "The pretender",
"price": 42.0,
"tax": 3.2
}
```
✋️ 👆 💪 📣 💗 💪 🔢, ✅ `item` &amp; `user`:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="22"
{!> ../../../docs_src/body_multiple_params/tutorial002.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="20"
{!> ../../../docs_src/body_multiple_params/tutorial002_py310.py!}
```
👉 💼, **FastAPI** 🔜 👀 👈 📤 🌅 🌘 1⃣ 💪 🔢 🔢 (2⃣ 🔢 👈 Pydantic 🏷).
, ⚫️ 🔜 ⤴️ ⚙️ 🔢 📛 🔑 (🏑 📛) 💪, &amp; ⌛ 💪 💖:
```JSON
{
"item": {
"name": "Foo",
"description": "The pretender",
"price": 42.0,
"tax": 3.2
},
"user": {
"username": "dave",
"full_name": "Dave Grohl"
}
}
```
!!! note
👀 👈 ✋️ `item` 📣 🎏 🌌 ⏭, ⚫️ 🔜 ⌛ 🔘 💪 ⏮️ 🔑 `item`.
**FastAPI** 🔜 🏧 🛠️ ⚪️➡️ 📨, 👈 🔢 `item` 📨 ⚫️ 🎯 🎚 &amp; 🎏 `user`.
⚫️ 🔜 🎭 🔬 ⚗ 💽, &amp; 🔜 📄 ⚫️ 💖 👈 🗄 🔗 &amp; 🏧 🩺.
## ⭐ 💲 💪
🎏 🌌 📤 `Query` &amp; `Path` 🔬 💽 🔢 &amp; ➡ 🔢, **FastAPI** 🚚 🌓 `Body`.
🖼, ↔ ⏮️ 🏷, 👆 💪 💭 👈 👆 💚 ✔️ 1⃣ 🔑 `importance` 🎏 💪, 🥈 `item` &amp; `user`.
🚥 👆 📣 ⚫️, ↩️ ⚫️ ⭐ 💲, **FastAPI** 🔜 🤔 👈 ⚫️ 🔢 🔢.
✋️ 👆 💪 💡 **FastAPI** 😥 ⚫️ 1⃣ 💪 🔑 ⚙️ `Body`:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="22"
{!> ../../../docs_src/body_multiple_params/tutorial003.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="20"
{!> ../../../docs_src/body_multiple_params/tutorial003_py310.py!}
```
👉 💼, **FastAPI** 🔜 ⌛ 💪 💖:
```JSON
{
"item": {
"name": "Foo",
"description": "The pretender",
"price": 42.0,
"tax": 3.2
},
"user": {
"username": "dave",
"full_name": "Dave Grohl"
},
"importance": 5
}
```
🔄, ⚫️ 🔜 🗜 📊 🆎, ✔, 📄, ♒️.
## 💗 💪 = &amp; 🔢
↗️, 👆 💪 📣 🌖 🔢 🔢 🕐❔ 👆 💪, 🌖 🙆 💪 🔢.
, 🔢, ⭐ 💲 🔬 🔢 🔢, 👆 🚫 ✔️ 🎯 🚮 `Query`, 👆 💪:
```Python
q: Union[str, None] = None
```
⚖️ 🐍 3⃣.1⃣0⃣ &amp; 🔛:
```Python
q: str | None = None
```
🖼:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="27"
{!> ../../../docs_src/body_multiple_params/tutorial004.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="26"
{!> ../../../docs_src/body_multiple_params/tutorial004_py310.py!}
```
!!! info
`Body` ✔️ 🌐 🎏 🔬 &amp; 🗃 🔢 `Query`,`Path` &amp; 🎏 👆 🔜 👀 ⏪.
## ⏯ 👁 💪 🔢
➡️ 💬 👆 🕴 ✔️ 👁 `item` 💪 🔢 ⚪️➡️ Pydantic 🏷 `Item`.
🔢, **FastAPI** 🔜 ⤴️ ⌛ 🚮 💪 🔗.
✋️ 🚥 👆 💚 ⚫️ ⌛ 🎻 ⏮️ 🔑 `item` &amp; 🔘 ⚫️ 🏷 🎚, ⚫️ 🔨 🕐❔ 👆 📣 💪 🔢, 👆 💪 ⚙️ 🎁 `Body` 🔢 `embed`:
```Python
item: Item = Body(embed=True)
```
:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="17"
{!> ../../../docs_src/body_multiple_params/tutorial005.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="15"
{!> ../../../docs_src/body_multiple_params/tutorial005_py310.py!}
```
👉 💼 **FastAPI** 🔜 ⌛ 💪 💖:
```JSON hl_lines="2"
{
"item": {
"name": "Foo",
"description": "The pretender",
"price": 42.0,
"tax": 3.2
}
}
```
↩️:
```JSON
{
"name": "Foo",
"description": "The pretender",
"price": 42.0,
"tax": 3.2
}
```
## 🌃
👆 💪 🚮 💗 💪 🔢 👆 *➡ 🛠️ 🔢*, ✋️ 📨 💪 🕴 ✔️ 👁 💪.
✋️ **FastAPI** 🔜 🍵 ⚫️, 🤝 👆 ☑ 📊 👆 🔢, &amp; ✔ &amp; 📄 ☑ 🔗 *➡ 🛠️*.
👆 💪 📣 ⭐ 💲 📨 🍕 💪.
&amp; 👆 💪 💡 **FastAPI** ⏯ 💪 🔑 🕐❔ 📤 🕴 👁 🔢 📣.

View File

@@ -0,0 +1,382 @@
# 💪 - 🔁 🏷
⏮️ **FastAPI**, 👆 💪 🔬, ✔, 📄, &amp; ⚙️ 🎲 🙇 🐦 🏷 (👏 Pydantic).
## 📇 🏑
👆 💪 🔬 🔢 🏾. 🖼, 🐍 `list`:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="14"
{!> ../../../docs_src/body_nested_models/tutorial001.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="12"
{!> ../../../docs_src/body_nested_models/tutorial001_py310.py!}
```
👉 🔜 ⚒ `tags` 📇, 👐 ⚫️ 🚫 📣 🆎 🔣 📇.
## 📇 🏑 ⏮️ 🆎 🔢
✋️ 🐍 ✔️ 🎯 🌌 📣 📇 ⏮️ 🔗 🆎, ⚖️ "🆎 🔢":
### 🗄 ⌨ `List`
🐍 3⃣.9️⃣ &amp; 🔛 👆 💪 ⚙️ 🐩 `list` 📣 👫 🆎 ✍ 👥 🔜 👀 🔛. 👶
✋️ 🐍 ⏬ ⏭ 3⃣.9️⃣ (3⃣.6️⃣ &amp; 🔛), 👆 🥇 💪 🗄 `List` ⚪️➡️ 🐩 🐍 `typing` 🕹:
```Python hl_lines="1"
{!> ../../../docs_src/body_nested_models/tutorial002.py!}
```
### 📣 `list` ⏮️ 🆎 🔢
📣 🆎 👈 ✔️ 🆎 🔢 (🔗 🆎), 💖 `list`, `dict`, `tuple`:
* 🚥 👆 🐍 ⏬ 🔅 🌘 3⃣.9️⃣, 🗄 👫 🌓 ⏬ ⚪️➡️ `typing` 🕹
* 🚶‍♀️ 🔗 🆎(Ⓜ) "🆎 🔢" ⚙️ ⬜ 🗜: `[` &amp; `]`
🐍 3⃣.9️⃣ ⚫️ 🔜:
```Python
my_list: list[str]
```
⏬ 🐍 ⏭ 3⃣.9️⃣, ⚫️ 🔜:
```Python
from typing import List
my_list: List[str]
```
👈 🌐 🐩 🐍 ❕ 🆎 📄.
⚙️ 👈 🎏 🐩 ❕ 🏷 🔢 ⏮️ 🔗 🆎.
, 👆 🖼, 👥 💪 ⚒ `tags` 🎯 "📇 🎻":
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="14"
{!> ../../../docs_src/body_nested_models/tutorial002.py!}
```
=== "🐍 3⃣.9️⃣ &amp; 🔛"
```Python hl_lines="14"
{!> ../../../docs_src/body_nested_models/tutorial002_py39.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="12"
{!> ../../../docs_src/body_nested_models/tutorial002_py310.py!}
```
## ⚒ 🆎
✋️ ⤴️ 👥 💭 🔃 ⚫️, &amp; 🤔 👈 🔖 🚫🔜 🚫 🔁, 👫 🔜 🎲 😍 🎻.
&amp; 🐍 ✔️ 🎁 💽 🆎 ⚒ 😍 🏬, `set`.
⤴️ 👥 💪 📣 `tags` ⚒ 🎻:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="1 14"
{!> ../../../docs_src/body_nested_models/tutorial003.py!}
```
=== "🐍 3⃣.9️⃣ &amp; 🔛"
```Python hl_lines="14"
{!> ../../../docs_src/body_nested_models/tutorial003_py39.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="12"
{!> ../../../docs_src/body_nested_models/tutorial003_py310.py!}
```
⏮️ 👉, 🚥 👆 📨 📨 ⏮️ ❎ 📊, ⚫️ 🔜 🗜 ⚒ 😍 🏬.
&amp; 🕐❔ 👆 🔢 👈 📊, 🚥 ✔️ ❎, ⚫️ 🔜 🔢 ⚒ 😍 🏬.
&amp; ⚫️ 🔜 ✍ / 📄 ➡️ 💁‍♂️.
## 🐦 🏷
🔠 🔢 Pydantic 🏷 ✔️ 🆎.
✋️ 👈 🆎 💪 ⚫️ 1⃣ Pydantic 🏷.
, 👆 💪 📣 🙇 🐦 🎻 "🎚" ⏮️ 🎯 🔢 📛, 🆎 &amp; 🔬.
🌐 👈, 🎲 🐦.
### 🔬 📊
🖼, 👥 💪 🔬 `Image` 🏷:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="9-11"
{!> ../../../docs_src/body_nested_models/tutorial004.py!}
```
=== "🐍 3⃣.9️⃣ &amp; 🔛"
```Python hl_lines="9-11"
{!> ../../../docs_src/body_nested_models/tutorial004_py39.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="7-9"
{!> ../../../docs_src/body_nested_models/tutorial004_py310.py!}
```
### ⚙️ 📊 🆎
&amp; ⤴️ 👥 💪 ⚙️ ⚫️ 🆎 🔢:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="20"
{!> ../../../docs_src/body_nested_models/tutorial004.py!}
```
=== "🐍 3⃣.9️⃣ &amp; 🔛"
```Python hl_lines="20"
{!> ../../../docs_src/body_nested_models/tutorial004_py39.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="18"
{!> ../../../docs_src/body_nested_models/tutorial004_py310.py!}
```
👉 🔜 ⛓ 👈 **FastAPI** 🔜 ⌛ 💪 🎏:
```JSON
{
"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"
}
}
```
🔄, 🤸 👈 📄, ⏮️ **FastAPI** 👆 🤚:
* 👨‍🎨 🐕‍🦺 (🛠️, ♒️), 🐦 🏷
* 💽 🛠️
* 💽 🔬
* 🏧 🧾
## 🎁 🆎 &amp; 🔬
↖️ ⚪️➡️ 😐 ⭐ 🆎 💖 `str`, `int`, `float`, ♒️. 👆 💪 ⚙️ 🌅 🏗 ⭐ 🆎 👈 😖 ⚪️➡️ `str`.
👀 🌐 🎛 👆 ✔️, 🛒 🩺 <a href="https://pydantic-docs.helpmanual.io/usage/types/" class="external-link" target="_blank">Pydantic 😍 🆎</a>. 👆 🔜 👀 🖼 ⏭ 📃.
🖼, `Image` 🏷 👥 ✔️ `url` 🏑, 👥 💪 📣 ⚫️ ↩️ `str`, Pydantic `HttpUrl`:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="4 10"
{!> ../../../docs_src/body_nested_models/tutorial005.py!}
```
=== "🐍 3⃣.9️⃣ &amp; 🔛"
```Python hl_lines="4 10"
{!> ../../../docs_src/body_nested_models/tutorial005_py39.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="2 8"
{!> ../../../docs_src/body_nested_models/tutorial005_py310.py!}
```
🎻 🔜 ✅ ☑ 📛, &amp; 📄 🎻 🔗 / 🗄 ✅.
## 🔢 ⏮️ 📇 📊
👆 💪 ⚙️ Pydantic 🏷 🏾 `list`, `set`, ♒️:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="20"
{!> ../../../docs_src/body_nested_models/tutorial006.py!}
```
=== "🐍 3⃣.9️⃣ &amp; 🔛"
```Python hl_lines="20"
{!> ../../../docs_src/body_nested_models/tutorial006_py39.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="18"
{!> ../../../docs_src/body_nested_models/tutorial006_py310.py!}
```
👉 🔜 ⌛ (🗜, ✔, 📄, ♒️) 🎻 💪 💖:
```JSON hl_lines="11"
{
"name": "Foo",
"description": "The pretender",
"price": 42.0,
"tax": 3.2,
"tags": [
"rock",
"metal",
"bar"
],
"images": [
{
"url": "http://example.com/baz.jpg",
"name": "The Foo live"
},
{
"url": "http://example.com/dave.jpg",
"name": "The Baz"
}
]
}
```
!!! info
👀 ❔ `images` 🔑 🔜 ✔️ 📇 🖼 🎚.
## 🙇 🐦 🏷
👆 💪 🔬 🎲 🙇 🐦 🏷:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="9 14 20 23 27"
{!> ../../../docs_src/body_nested_models/tutorial007.py!}
```
=== "🐍 3⃣.9️⃣ &amp; 🔛"
```Python hl_lines="9 14 20 23 27"
{!> ../../../docs_src/body_nested_models/tutorial007_py39.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="7 12 18 21 25"
{!> ../../../docs_src/body_nested_models/tutorial007_py310.py!}
```
!!! info
👀 ❔ `Offer` ✔️ 📇 `Item`Ⓜ, ❔ 🔄 ✔️ 📦 📇 `Image`Ⓜ
## 💪 😁 📇
🚥 🔝 🎚 💲 🎻 💪 👆 ⌛ 🎻 `array` (🐍 `list`), 👆 💪 📣 🆎 🔢 🔢, 🎏 Pydantic 🏷:
```Python
images: List[Image]
```
⚖️ 🐍 3⃣.9️⃣ &amp; 🔛:
```Python
images: list[Image]
```
:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="15"
{!> ../../../docs_src/body_nested_models/tutorial008.py!}
```
=== "🐍 3⃣.9️⃣ &amp; 🔛"
```Python hl_lines="13"
{!> ../../../docs_src/body_nested_models/tutorial008_py39.py!}
```
## 👨‍🎨 🐕‍🦺 🌐
&amp; 👆 🤚 👨‍🎨 🐕‍🦺 🌐.
🏬 🔘 📇:
<img src="/img/tutorial/body-nested-models/image01.png">
👆 🚫 🚫 🤚 👉 😇 👨‍🎨 🐕‍🦺 🚥 👆 👷 🔗 ⏮️ `dict` ↩️ Pydantic 🏷.
✋️ 👆 🚫 ✔️ 😟 🔃 👫 👯‍♂️, 📨 #️⃣ 🗜 🔁 &amp; 👆 🔢 🗜 🔁 🎻 💁‍♂️.
## 💪 ❌ `dict`Ⓜ
👆 💪 📣 💪 `dict` ⏮️ 🔑 🆎 &amp; 💲 🎏 🆎.
🍵 ✔️ 💭 ⏪ ⚫️❔ ☑ 🏑/🔢 📛 (🔜 💼 ⏮️ Pydantic 🏷).
👉 🔜 ⚠ 🚥 👆 💚 📨 🔑 👈 👆 🚫 ⏪ 💭.
---
🎏 ⚠ 💼 🕐❔ 👆 💚 ✔️ 🔑 🎏 🆎, ✅ `int`.
👈 ⚫️❔ 👥 🔜 👀 📥.
👉 💼, 👆 🔜 🚫 🙆 `dict` 📏 ⚫️ ✔️ `int` 🔑 ⏮️ `float` 💲:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="9"
{!> ../../../docs_src/body_nested_models/tutorial009.py!}
```
=== "🐍 3⃣.9️⃣ &amp; 🔛"
```Python hl_lines="7"
{!> ../../../docs_src/body_nested_models/tutorial009_py39.py!}
```
!!! tip
✔️ 🤯 👈 🎻 🕴 🐕‍🦺 `str` 🔑.
✋️ Pydantic ✔️ 🏧 💽 🛠️.
👉 ⛓ 👈, ✋️ 👆 🛠️ 👩‍💻 💪 🕴 📨 🎻 🔑, 📏 👈 🎻 🔌 😁 🔢, Pydantic 🔜 🗜 👫 &amp; ✔ 👫.
&amp; `dict` 👆 📨 `weights` 🔜 🤙 ✔️ `int` 🔑 &amp; `float` 💲.
## 🌃
⏮️ **FastAPI** 👆 ✔️ 🔆 💪 🚚 Pydantic 🏷, ⏪ 🚧 👆 📟 🙅, 📏 &amp; 😍.
✋️ ⏮️ 🌐 💰:
* 👨‍🎨 🐕‍🦺 (🛠️ 🌐 ❗)
* 💽 🛠️ (.Ⓜ.. ✍ / 🛠️)
* 💽 🔬
* 🔗 🧾
* 🏧 🩺

View File

@@ -0,0 +1,155 @@
# 💪 -
## ❎ ⏮️ `PUT`
🏬 👆 💪 ⚙️ <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PUT" class="external-link" target="_blank">🇺🇸🔍 `PUT`</a> 🛠️.
👆 💪 ⚙️ `jsonable_encoder` 🗜 🔢 💽 📊 👈 💪 🏪 🎻 (✅ ⏮️ ☁ 💽). 🖼, 🏭 `datetime` `str`.
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="30-35"
{!> ../../../docs_src/body_updates/tutorial001.py!}
```
=== "🐍 3⃣.9️⃣ &amp; 🔛"
```Python hl_lines="30-35"
{!> ../../../docs_src/body_updates/tutorial001_py39.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="28-33"
{!> ../../../docs_src/body_updates/tutorial001_py310.py!}
```
`PUT` ⚙️ 📨 💽 👈 🔜 ❎ ♻ 💽.
### ⚠ 🔃 ❎
👈 ⛓ 👈 🚥 👆 💚 🏬 `bar` ⚙️ `PUT` ⏮️ 💪 ⚗:
```Python
{
"name": "Barz",
"price": 3,
"description": None,
}
```
↩️ ⚫️ 🚫 🔌 ⏪ 🏪 🔢 `"tax": 20.2`, 🔢 🏷 🔜 ✊ 🔢 💲 `"tax": 10.5`.
&amp; 📊 🔜 🖊 ⏮️ 👈 "🆕" `tax` `10.5`.
## 🍕 ⏮️ `PATCH`
👆 💪 ⚙️ <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PATCH" class="external-link" target="_blank">🇺🇸🔍 `PATCH`</a> 🛠️ *🍕* 💽.
👉 ⛓ 👈 👆 💪 📨 🕴 💽 👈 👆 💚 , 🍂 🎂 🐣.
!!! Note
`PATCH` 🌘 🛎 ⚙️ &amp; 💭 🌘 `PUT`.
&amp; 📚 🏉 ⚙️ 🕴 `PUT`, 🍕 .
👆 **🆓** ⚙️ 👫 👐 👆 💚, **FastAPI** 🚫 🚫 🙆 🚫.
✋️ 👉 🦮 🎦 👆, 🌖 ⚖️ 🌘, ❔ 👫 🎯 ⚙️.
### ⚙️ Pydantic `exclude_unset` 🔢
🚥 👆 💚 📨 🍕 , ⚫️ 📶 ⚠ ⚙️ 🔢 `exclude_unset` Pydantic 🏷 `.dict()`.
💖 `item.dict(exclude_unset=True)`.
👈 🔜 🏗 `dict` ⏮️ 🕴 💽 👈 ⚒ 🕐❔ 🏗 `item` 🏷, 🚫 🔢 💲.
⤴️ 👆 💪 ⚙️ 👉 🏗 `dict` ⏮️ 🕴 💽 👈 ⚒ (📨 📨), 🚫 🔢 💲:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="34"
{!> ../../../docs_src/body_updates/tutorial002.py!}
```
=== "🐍 3⃣.9️⃣ &amp; 🔛"
```Python hl_lines="34"
{!> ../../../docs_src/body_updates/tutorial002_py39.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="32"
{!> ../../../docs_src/body_updates/tutorial002_py310.py!}
```
### ⚙️ Pydantic `update` 🔢
🔜, 👆 💪 ✍ 📁 ♻ 🏷 ⚙️ `.copy()`, &amp; 🚶‍♀️ `update` 🔢 ⏮️ `dict` ⚗ 💽 .
💖 `stored_item_model.copy(update=update_data)`:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="35"
{!> ../../../docs_src/body_updates/tutorial002.py!}
```
=== "🐍 3⃣.9️⃣ &amp; 🔛"
```Python hl_lines="35"
{!> ../../../docs_src/body_updates/tutorial002_py39.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="33"
{!> ../../../docs_src/body_updates/tutorial002_py310.py!}
```
### 🍕 🌃
📄, ✔ 🍕 👆 🔜:
* (⚗) ⚙️ `PATCH` ↩️ `PUT`.
* 🗃 🏪 💽.
* 🚮 👈 💽 Pydantic 🏷.
* 🏗 `dict` 🍵 🔢 💲 ⚪️➡️ 🔢 🏷 (⚙️ `exclude_unset`).
* 👉 🌌 👆 💪 🕴 💲 🤙 ⚒ 👩‍💻, ↩️ 🔐 💲 ⏪ 🏪 ⏮️ 🔢 💲 👆 🏷.
* ✍ 📁 🏪 🏷, 🛠️ ⚫️ 🔢 ⏮️ 📨 🍕 (⚙️ `update` 🔢).
* 🗜 📁 🏷 🕳 👈 💪 🏪 👆 💽 (🖼, ⚙️ `jsonable_encoder`).
* 👉 ⭐ ⚙️ 🏷 `.dict()` 👩‍🔬 🔄, ✋️ ⚫️ ⚒ 💭 (&amp; 🗜) 💲 💽 🆎 👈 💪 🗜 🎻, 🖼, `datetime` `str`.
* 🖊 💽 👆 💽.
* 📨 🏷.
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="30-37"
{!> ../../../docs_src/body_updates/tutorial002.py!}
```
=== "🐍 3⃣.9️⃣ &amp; 🔛"
```Python hl_lines="30-37"
{!> ../../../docs_src/body_updates/tutorial002_py39.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="28-35"
{!> ../../../docs_src/body_updates/tutorial002_py310.py!}
```
!!! tip
👆 💪 🤙 ⚙️ 👉 🎏 ⚒ ⏮️ 🇺🇸🔍 `PUT` 🛠️.
✋️ 🖼 📥 ⚙️ `PATCH` ↩️ ⚫️ ✍ 👫 ⚙️ 💼.
!!! note
👀 👈 🔢 🏷 ✔.
, 🚥 👆 💚 📨 🍕 👈 💪 🚫 🌐 🔢, 👆 💪 ✔️ 🏷 ⏮️ 🌐 🔢 ™ 📦 (⏮️ 🔢 💲 ⚖️ `None`).
🔬 ⚪️➡️ 🏷 ⏮️ 🌐 📦 💲 **** &amp; 🏷 ⏮️ ✔ 💲 **🏗**, 👆 💪 ⚙️ 💭 🔬 [ 🏷](extra-models.md){.internal-link target=_blank}.

View File

@@ -0,0 +1,213 @@
# 📨 💪
🕐❔ 👆 💪 📨 📊 ⚪️➡️ 👩‍💻 (➡️ 💬, 🖥) 👆 🛠️, 👆 📨 ⚫️ **📨 💪**.
**📨** 💪 📊 📨 👩‍💻 👆 🛠️. **📨** 💪 💽 👆 🛠️ 📨 👩‍💻.
👆 🛠️ 🌖 🕧 ✔️ 📨 **📨** 💪. ✋️ 👩‍💻 🚫 🎯 💪 📨 **📨** 💪 🌐 🕰.
📣 **📨** 💪, 👆 ⚙️ <a href="https://pydantic-docs.helpmanual.io/" class="external-link" target="_blank">Pydantic</a> 🏷 ⏮️ 🌐 👫 🏋️ &amp; 💰.
!!! info
📨 💽, 👆 🔜 ⚙️ 1⃣: `POST` (🌅 ⚠), `PUT`, `DELETE` ⚖️ `PATCH`.
📨 💪 ⏮️ `GET` 📨 ✔️ ⚠ 🎭 🔧, 👐, ⚫️ 🐕‍🦺 FastAPI, 🕴 📶 🏗/😕 ⚙️ 💼.
⚫️ 🚫, 🎓 🩺 ⏮️ 🦁 🎚 🏆 🚫 🎦 🧾 💪 🕐❔ ⚙️ `GET`, &amp; 🗳 🖕 💪 🚫 🐕‍🦺 ⚫️.
## 🗄 Pydantic `BaseModel`
🥇, 👆 💪 🗄 `BaseModel` ⚪️➡️ `pydantic`:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="4"
{!> ../../../docs_src/body/tutorial001.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="2"
{!> ../../../docs_src/body/tutorial001_py310.py!}
```
## ✍ 👆 💽 🏷
⤴️ 👆 📣 👆 💽 🏷 🎓 👈 😖 ⚪️➡️ `BaseModel`.
⚙️ 🐩 🐍 🆎 🌐 🔢:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="7-11"
{!> ../../../docs_src/body/tutorial001.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="5-9"
{!> ../../../docs_src/body/tutorial001_py310.py!}
```
🎏 🕐❔ 📣 🔢 🔢, 🕐❔ 🏷 🔢 ✔️ 🔢 💲, ⚫️ 🚫 ✔. ⏪, ⚫️ ✔. ⚙️ `None` ⚒ ⚫️ 📦.
🖼, 👉 🏷 🔛 📣 🎻 "`object`" (⚖️ 🐍 `dict`) 💖:
```JSON
{
"name": "Foo",
"description": "An optional description",
"price": 45.2,
"tax": 3.5
}
```
... `description` &amp; `tax` 📦 (⏮️ 🔢 💲 `None`), 👉 🎻 "`object`" 🔜 ☑:
```JSON
{
"name": "Foo",
"price": 45.2
}
```
## 📣 ⚫️ 🔢
🚮 ⚫️ 👆 *➡ 🛠️*, 📣 ⚫️ 🎏 🌌 👆 📣 ➡ &amp; 🔢 🔢:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="18"
{!> ../../../docs_src/body/tutorial001.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="16"
{!> ../../../docs_src/body/tutorial001_py310.py!}
```
...&amp; 📣 🚮 🆎 🏷 👆 ✍, `Item`.
## 🏁
⏮️ 👈 🐍 🆎 📄, **FastAPI** 🔜:
* ✍ 💪 📨 🎻.
* 🗜 🔗 🆎 (🚥 💪).
* ✔ 💽.
* 🚥 💽 ❌, ⚫️ 🔜 📨 👌 &amp; 🆑 ❌, ☠️ ⚫️❔ 🌐❔ &amp; ⚫️❔ ❌ 📊.
* 🤝 👆 📨 📊 🔢 `item`.
* 👆 📣 ⚫️ 🔢 🆎 `Item`, 👆 🔜 ✔️ 🌐 👨‍🎨 🐕‍🦺 (🛠️, ♒️) 🌐 🔢 &amp; 👫 🆎.
* 🏗 <a href="https://json-schema.org" class="external-link" target="_blank">🎻 🔗</a> 🔑 👆 🏷, 👆 💪 ⚙️ 👫 🙆 🙆 👆 💖 🚥 ⚫️ ⚒ 🔑 👆 🏗.
* 👈 🔗 🔜 🍕 🏗 🗄 🔗, &amp; ⚙️ 🏧 🧾 <abbr title="User Interfaces">⚜</abbr>.
## 🏧 🩺
🎻 🔗 👆 🏷 🔜 🍕 👆 🗄 🏗 🔗, &amp; 🔜 🎦 🎓 🛠️ 🩺:
<img src="/img/tutorial/body/image01.png">
&amp; 🔜 ⚙️ 🛠️ 🩺 🔘 🔠 *➡ 🛠️* 👈 💪 👫:
<img src="/img/tutorial/body/image02.png">
## 👨‍🎨 🐕‍🦺
👆 👨‍🎨, 🔘 👆 🔢 👆 🔜 🤚 🆎 🔑 &amp; 🛠️ 🌐 (👉 🚫🔜 🔨 🚥 👆 📨 `dict` ↩️ Pydantic 🏷):
<img src="/img/tutorial/body/image03.png">
👆 🤚 ❌ ✅ ❌ 🆎 🛠️:
<img src="/img/tutorial/body/image04.png">
👉 🚫 🤞, 🎂 🛠️ 🏗 🤭 👈 🔧.
&amp; ⚫️ 🙇 💯 🔧 🌓, ⏭ 🙆 🛠️, 🚚 ⚫️ 🔜 👷 ⏮️ 🌐 👨‍🎨.
📤 🔀 Pydantic ⚫️ 🐕‍🦺 👉.
⏮️ 🖼 ✊ ⏮️ <a href="https://code.visualstudio.com" class="external-link" target="_blank">🎙 🎙 📟</a>.
✋️ 👆 🔜 🤚 🎏 👨‍🎨 🐕‍🦺 ⏮️ <a href="https://www.jetbrains.com/pycharm/" class="external-link" target="_blank">🗒</a> &amp; 🌅 🎏 🐍 👨‍🎨:
<img src="/img/tutorial/body/image05.png">
!!! tip
🚥 👆 ⚙️ <a href="https://www.jetbrains.com/pycharm/" class="external-link" target="_blank">🗒</a> 👆 👨‍🎨, 👆 💪 ⚙️ <a href="https://github.com/koxudaxi/pydantic-pycharm-plugin/" class="external-link" target="_blank">Pydantic 🗒 📁</a>.
⚫️ 📉 👨‍🎨 🐕‍🦺 Pydantic 🏷, ⏮️:
* 🚘-🛠️
* 🆎 ✅
* 🛠️
* 🔎
* 🔬
## ⚙️ 🏷
🔘 🔢, 👆 💪 🔐 🌐 🔢 🏷 🎚 🔗:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="21"
{!> ../../../docs_src/body/tutorial002.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="19"
{!> ../../../docs_src/body/tutorial002_py310.py!}
```
## 📨 💪 ➡ 🔢
👆 💪 📣 ➡ 🔢 &amp; 📨 💪 🎏 🕰.
**FastAPI** 🔜 🤔 👈 🔢 🔢 👈 🏏 ➡ 🔢 🔜 **✊ ⚪️➡️ ➡**, &amp; 👈 🔢 🔢 👈 📣 Pydantic 🏷 🔜 **✊ ⚪️➡️ 📨 💪**.
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="17-18"
{!> ../../../docs_src/body/tutorial003.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="15-16"
{!> ../../../docs_src/body/tutorial003_py310.py!}
```
## 📨 💪 🔢 🔢
👆 💪 📣 **💪**, **➡** &amp; **🔢** 🔢, 🌐 🎏 🕰.
**FastAPI** 🔜 🤔 🔠 👫 &amp; ✊ 📊 ⚪️➡️ ☑ 🥉.
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="18"
{!> ../../../docs_src/body/tutorial004.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="16"
{!> ../../../docs_src/body/tutorial004_py310.py!}
```
🔢 🔢 🔜 🤔 ⏩:
* 🚥 🔢 📣 **➡**, ⚫️ 🔜 ⚙️ ➡ 🔢.
* 🚥 🔢 **⭐ 🆎** (💖 `int`, `float`, `str`, `bool`, ♒️) ⚫️ 🔜 🔬 **🔢** 🔢.
* 🚥 🔢 📣 🆎 **Pydantic 🏷**, ⚫️ 🔜 🔬 📨 **💪**.
!!! note
FastAPI 🔜 💭 👈 💲 `q` 🚫 ✔ ↩️ 🔢 💲 `= None`.
`Union` `Union[str, None]` 🚫 ⚙️ FastAPI, ✋️ 🔜 ✔ 👆 👨‍🎨 🤝 👆 👍 🐕‍🦺 &amp; 🔍 ❌.
## 🍵 Pydantic
🚥 👆 🚫 💚 ⚙️ Pydantic 🏷, 👆 💪 ⚙️ **💪** 🔢. 👀 🩺 [💪 - 💗 🔢: ⭐ 💲 💪](body-multiple-params.md#singular-values-in-body){.internal-link target=_blank}.

View File

@@ -0,0 +1,49 @@
# 🍪 🔢
👆 💪 🔬 🍪 🔢 🎏 🌌 👆 🔬 `Query` &amp; `Path` 🔢.
## 🗄 `Cookie`
🥇 🗄 `Cookie`:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="3"
{!> ../../../docs_src/cookie_params/tutorial001.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="1"
{!> ../../../docs_src/cookie_params/tutorial001_py310.py!}
```
## 📣 `Cookie` 🔢
⤴️ 📣 🍪 🔢 ⚙️ 🎏 📊 ⏮️ `Path` &amp; `Query`.
🥇 💲 🔢 💲, 👆 💪 🚶‍♀️ 🌐 🔬 ⚖️ ✍ 🔢:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="9"
{!> ../../../docs_src/cookie_params/tutorial001.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="7"
{!> ../../../docs_src/cookie_params/tutorial001_py310.py!}
```
!!! note "📡 "
`Cookie` "👭" 🎓 `Path` &amp; `Query`. ⚫️ 😖 ⚪️➡️ 🎏 ⚠ `Param` 🎓.
✋️ 💭 👈 🕐❔ 👆 🗄 `Query`, `Path`, `Cookie` &amp; 🎏 ⚪️➡️ `fastapi`, 👈 🤙 🔢 👈 📨 🎁 🎓.
!!! info
📣 🍪, 👆 💪 ⚙️ `Cookie`, ↩️ ⏪ 🔢 🔜 🔬 🔢 🔢.
## 🌃
📣 🍪 ⏮️ `Cookie`, ⚙️ 🎏 ⚠ ⚓ `Query` &amp; `Path`.

View File

@@ -0,0 +1,84 @@
# ⚜ (✖️-🇨🇳 🤝)
<a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS" class="external-link" target="_blank">⚜ ⚖️ "✖️-🇨🇳 🤝"</a> 🔗 ⚠ 🕐❔ 🕸 🏃‍♂ 🖥 ✔️ 🕸 📟 👈 🔗 ⏮️ 👩‍💻, &amp; 👩‍💻 🎏 "🇨🇳" 🌘 🕸.
## 🇨🇳
🇨🇳 🌀 🛠️ (`http`, `https`), 🆔 (`myapp.com`, `localhost`, `localhost.tiangolo.com`), &amp; ⛴ (`80`, `443`, `8080`).
, 🌐 👫 🎏 🇨🇳:
* `http://localhost`
* `https://localhost`
* `http://localhost:8080`
🚥 👫 🌐 `localhost`, 👫 ⚙️ 🎏 🛠️ ⚖️ ⛴,, 👫 🎏 "🇨🇳".
## 🔁
, ➡️ 💬 👆 ✔️ 🕸 🏃 👆 🖥 `http://localhost:8080`, &amp; 🚮 🕸 🔄 🔗 ⏮️ 👩‍💻 🏃 `http://localhost` (↩️ 👥 🚫 ✔ ⛴, 🖥 🔜 🤔 🔢 ⛴ `80`).
⤴️, 🖥 🔜 📨 🇺🇸🔍 `OPTIONS` 📨 👩‍💻, &amp; 🚥 👩‍💻 📨 ☑ 🎚 ✔ 📻 ⚪️➡️ 👉 🎏 🇨🇳 (`http://localhost:8080`) ⤴️ 🖥 🔜 ➡️ 🕸 🕸 📨 🚮 📨 👩‍💻.
🏆 👉, 👩‍💻 🔜 ✔️ 📇 "✔ 🇨🇳".
👉 💼, ⚫️ 🔜 ✔️ 🔌 `http://localhost:8080` 🕸 👷 ☑.
## 🃏
⚫️ 💪 📣 📇 `"*"` ("🃏") 💬 👈 🌐 ✔.
✋️ 👈 🔜 🕴 ✔ 🎯 🆎 📻, 🚫 🌐 👈 🔌 🎓: 🍪, ✔ 🎚 💖 📚 ⚙️ ⏮️ 📨 🤝, ♒️.
, 🌐 👷 ☑, ⚫️ 👻 ✔ 🎯 ✔ 🇨🇳.
## ⚙️ `CORSMiddleware`
👆 💪 🔗 ⚫️ 👆 **FastAPI** 🈸 ⚙️ `CORSMiddleware`.
* 🗄 `CORSMiddleware`.
* ✍ 📇 ✔ 🇨🇳 (🎻).
* 🚮 ⚫️ "🛠️" 👆 **FastAPI** 🈸.
👆 💪 ✔ 🚥 👆 👩‍💻 ✔:
* 🎓 (✔ 🎚, 🍪, ♒️).
* 🎯 🇺🇸🔍 👩‍🔬 (`POST`, `PUT`) ⚖️ 🌐 👫 ⏮️ 🃏 `"*"`.
* 🎯 🇺🇸🔍 🎚 ⚖️ 🌐 👫 ⏮️ 🃏 `"*"`.
```Python hl_lines="2 6-11 13-19"
{!../../../docs_src/cors/tutorial001.py!}
```
🔢 🔢 ⚙️ `CORSMiddleware` 🛠️ 🚫 🔢, 👆 🔜 💪 🎯 🛠️ 🎯 🇨🇳, 👩‍🔬, ⚖️ 🎚, ✔ 🖥 ✔ ⚙️ 👫 ✖️-🆔 🔑.
📄 ❌ 🐕‍🦺:
* `allow_origins` - 📇 🇨🇳 👈 🔜 ✔ ⚒ ✖️-🇨🇳 📨. 🤶 Ⓜ. `['https://example.org', 'https://www.example.org']`. 👆 💪 ⚙️ `['*']` ✔ 🙆 🇨🇳.
* `allow_origin_regex` - 🎻 🎻 🏏 🛡 🇨🇳 👈 🔜 ✔ ⚒ ✖️-🇨🇳 📨. ✅ `'https://.*\.example\.org'`.
* `allow_methods` - 📇 🇺🇸🔍 👩‍🔬 👈 🔜 ✔ ✖️-🇨🇳 📨. 🔢 `['GET']`. 👆 💪 ⚙️ `['*']` ✔ 🌐 🐩 👩‍🔬.
* `allow_headers` - 📇 🇺🇸🔍 📨 🎚 👈 🔜 🐕‍🦺 ✖️-🇨🇳 📨. 🔢 `[]`. 👆 💪 ⚙️ `['*']` ✔ 🌐 🎚. `Accept`, `Accept-Language`, `Content-Language` &amp; `Content-Type` 🎚 🕧 ✔ <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS#simple_requests" class="external-link" rel="noopener" target="_blank">🙅 ⚜ 📨</a>.
* `allow_credentials` - 🎦 👈 🍪 🔜 🐕‍🦺 ✖️-🇨🇳 📨. 🔢 `False`. , `allow_origins` 🚫🔜 ⚒ `['*']` 🎓 ✔, 🇨🇳 🔜 ✔.
* `expose_headers` - 🎦 🙆 📨 🎚 👈 🔜 ⚒ ♿ 🖥. 🔢 `[]`.
* `max_age` - ⚒ 🔆 🕰 🥈 🖥 💾 ⚜ 📨. 🔢 `600`.
🛠️ 📨 2⃣ 🎯 🆎 🇺🇸🔍 📨...
### ⚜ 🛫 📨
👉 🙆 `OPTIONS` 📨 ⏮️ `Origin` &amp; `Access-Control-Request-Method` 🎚.
👉 💼 🛠️ 🔜 🆘 📨 📨 &amp; 📨 ⏮️ ☑ ⚜ 🎚, &amp; 👯‍♂️ `200` ⚖️ `400` 📨 🎓 🎯.
### 🙅 📨
🙆 📨 ⏮️ `Origin` 🎚. 👉 💼 🛠️ 🔜 🚶‍♀️ 📨 🔘 😐, ✋️ 🔜 🔌 ☑ ⚜ 🎚 🔛 📨.
## 🌅
🌖 🔃 <abbr title="Cross-Origin Resource Sharing">⚜</abbr>, ✅ <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS" class="external-link" target="_blank">🦎 ⚜ 🧾</a>.
!!! note "📡 "
👆 💪 ⚙️ `from starlette.middleware.cors import CORSMiddleware`.
**FastAPI** 🚚 📚 🛠️ `fastapi.middleware` 🏪 👆, 👩‍💻. ✋️ 🌅 💪 🛠️ 👟 🔗 ⚪️➡️ 💃.

View File

@@ -0,0 +1,112 @@
# 🛠️
👆 💪 🔗 🕹 👆 👨‍🎨, 🖼 ⏮️ 🎙 🎙 📟 ⚖️ 🗒.
## 🤙 `uvicorn`
👆 FastAPI 🈸, 🗄 &amp; 🏃 `uvicorn` 🔗:
```Python hl_lines="1 15"
{!../../../docs_src/debugging/tutorial001.py!}
```
### 🔃 `__name__ == "__main__"`
👑 🎯 `__name__ == "__main__"` ✔️ 📟 👈 🛠️ 🕐❔ 👆 📁 🤙 ⏮️:
<div class="termy">
```console
$ python myapp.py
```
</div>
✋️ 🚫 🤙 🕐❔ 1⃣ 📁 🗄 ⚫️, 💖:
```Python
from myapp import app
```
#### 🌅
➡️ 💬 👆 📁 🌟 `myapp.py`.
🚥 👆 🏃 ⚫️ ⏮️:
<div class="termy">
```console
$ python myapp.py
```
</div>
⤴️ 🔗 🔢 `__name__` 👆 📁, ✍ 🔁 🐍, 🔜 ✔️ 💲 🎻 `"__main__"`.
, 📄:
```Python
uvicorn.run(app, host="0.0.0.0", port=8000)
```
🔜 🏃.
---
👉 🏆 🚫 🔨 🚥 👆 🗄 👈 🕹 (📁).
, 🚥 👆 ✔️ 1⃣ 📁 `importer.py` ⏮️:
```Python
from myapp import app
# Some more code
```
👈 💼, 🏧 🔢 🔘 `myapp.py` 🔜 🚫 ✔️ 🔢 `__name__` ⏮️ 💲 `"__main__"`.
, ⏸:
```Python
uvicorn.run(app, host="0.0.0.0", port=8000)
```
🔜 🚫 🛠️.
!!! info
🌅 , ✅ <a href="https://docs.python.org/3/library/__main__.html" class="external-link" target="_blank">🛂 🐍 🩺</a>.
## 🏃 👆 📟 ⏮️ 👆 🕹
↩️ 👆 🏃 Uvicorn 💽 🔗 ⚪️➡️ 👆 📟, 👆 💪 🤙 👆 🐍 📋 (👆 FastAPI 🈸) 🔗 ⚪️➡️ 🕹.
---
🖼, 🎙 🎙 📟, 👆 💪:
* 🚶 "" 🎛.
* "🚮 📳...".
* 🖊 "🐍"
* 🏃 🕹 ⏮️ 🎛 "`Python: Current File (Integrated Terminal)`".
⚫️ 🔜 ⤴️ ▶️ 💽 ⏮️ 👆 **FastAPI** 📟, ⛔️ 👆 0⃣, ♒️.
📥 ❔ ⚫️ 💪 👀:
<img src="/img/tutorial/debugging/image01.png">
---
🚥 👆 ⚙️ 🗒, 👆 💪:
* 📂 "🏃" 🍣.
* 🖊 🎛 "...".
* ⤴️ 🔑 🍣 🎦 🆙.
* 🖊 📁 (👉 💼, `main.py`).
⚫️ 🔜 ⤴️ ▶️ 💽 ⏮️ 👆 **FastAPI** 📟, ⛔️ 👆 0⃣, ♒️.
📥 ❔ ⚫️ 💪 👀:
<img src="/img/tutorial/debugging/image02.png">

View File

@@ -0,0 +1,247 @@
# 🎓 🔗
⏭ 🤿 ⏬ 🔘 **🔗 💉** ⚙️, ➡️ ♻ ⏮️ 🖼.
## `dict` ⚪️➡️ ⏮️ 🖼
⏮️ 🖼, 👥 🛬 `dict` ⚪️➡️ 👆 🔗 ("☑"):
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="9"
{!> ../../../docs_src/dependencies/tutorial001.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="7"
{!> ../../../docs_src/dependencies/tutorial001_py310.py!}
```
✋️ ⤴️ 👥 🤚 `dict` 🔢 `commons` *➡ 🛠️ 🔢*.
&amp; 👥 💭 👈 👨‍🎨 💪 🚫 🚚 📚 🐕‍🦺 (💖 🛠️) `dict`Ⓜ, ↩️ 👫 💪 🚫 💭 👫 🔑 &amp; 💲 🆎.
👥 💪 👍...
## ⚫️❔ ⚒ 🔗
🆙 🔜 👆 ✔️ 👀 🔗 📣 🔢.
✋️ 👈 🚫 🕴 🌌 📣 🔗 (👐 ⚫️ 🔜 🎲 🌖 ⚠).
🔑 ⚖ 👈 🔗 🔜 "🇧🇲".
"**🇧🇲**" 🐍 🕳 👈 🐍 💪 "🤙" 💖 🔢.
, 🚥 👆 ✔️ 🎚 `something` (👈 💪 _🚫_ 🔢) &amp; 👆 💪 "🤙" ⚫️ (🛠️ ⚫️) 💖:
```Python
something()
```
⚖️
```Python
something(some_argument, some_keyword_argument="foo")
```
⤴️ ⚫️ "🇧🇲".
## 🎓 🔗
👆 5⃣📆 👀 👈 ✍ 👐 🐍 🎓, 👆 ⚙️ 👈 🎏 ❕.
🖼:
```Python
class Cat:
def __init__(self, name: str):
self.name = name
fluffy = Cat(name="Mr Fluffy")
```
👉 💼, `fluffy` 👐 🎓 `Cat`.
&amp; ✍ `fluffy`, 👆 "🤙" `Cat`.
, 🐍 🎓 **🇧🇲**.
⤴️, **FastAPI**, 👆 💪 ⚙️ 🐍 🎓 🔗.
⚫️❔ FastAPI 🤙 ✅ 👈 ⚫️ "🇧🇲" (🔢, 🎓 ⚖️ 🕳 🙆) &amp; 🔢 🔬.
🚥 👆 🚶‍♀️ "🇧🇲" 🔗 **FastAPI**, ⚫️ 🔜 🔬 🔢 👈 "🇧🇲", &amp; 🛠️ 👫 🎏 🌌 🔢 *➡ 🛠️ 🔢*. ✅ 🎧-🔗.
👈 ✔ 🇧🇲 ⏮️ 🙅‍♂ 🔢 🌐. 🎏 ⚫️ 🔜 *➡ 🛠️ 🔢* ⏮️ 🙅‍♂ 🔢.
⤴️, 👥 💪 🔀 🔗 "☑" `common_parameters` ⚪️➡️ 🔛 🎓 `CommonQueryParams`:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="11-15"
{!> ../../../docs_src/dependencies/tutorial002.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="9-13"
{!> ../../../docs_src/dependencies/tutorial002_py310.py!}
```
💸 🙋 `__init__` 👩‍🔬 ⚙️ ✍ 👐 🎓:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="12"
{!> ../../../docs_src/dependencies/tutorial002.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="10"
{!> ../../../docs_src/dependencies/tutorial002_py310.py!}
```
...⚫️ ✔️ 🎏 🔢 👆 ⏮️ `common_parameters`:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="9"
{!> ../../../docs_src/dependencies/tutorial001.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="6"
{!> ../../../docs_src/dependencies/tutorial001_py310.py!}
```
📚 🔢 ⚫️❔ **FastAPI** 🔜 ⚙️ "❎" 🔗.
👯‍♂️ 💼, ⚫️ 🔜 ✔️:
* 📦 `q` 🔢 🔢 👈 `str`.
* `skip` 🔢 🔢 👈 `int`, ⏮️ 🔢 `0`.
* `limit` 🔢 🔢 👈 `int`, ⏮️ 🔢 `100`.
👯‍♂️ 💼 💽 🔜 🗜, ✔, 📄 🔛 🗄 🔗, ♒️.
## ⚙️ ⚫️
🔜 👆 💪 📣 👆 🔗 ⚙️ 👉 🎓.
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="19"
{!> ../../../docs_src/dependencies/tutorial002.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="17"
{!> ../../../docs_src/dependencies/tutorial002_py310.py!}
```
**FastAPI** 🤙 `CommonQueryParams` 🎓. 👉 ✍ "👐" 👈 🎓 &amp; 👐 🔜 🚶‍♀️ 🔢 `commons` 👆 🔢.
## 🆎 ✍ 🆚 `Depends`
👀 ❔ 👥 ✍ `CommonQueryParams` 🕐 🔛 📟:
```Python
commons: CommonQueryParams = Depends(CommonQueryParams)
```
🏁 `CommonQueryParams`,:
```Python
... = Depends(CommonQueryParams)
```
...⚫️❔ **FastAPI** 🔜 🤙 ⚙️ 💭 ⚫️❔ 🔗.
⚪️➡️ ⚫️ 👈 FastAPI 🔜 ⚗ 📣 🔢 &amp; 👈 ⚫️❔ FastAPI 🔜 🤙 🤙.
---
👉 💼, 🥇 `CommonQueryParams`,:
```Python
commons: CommonQueryParams ...
```
...🚫 ✔️ 🙆 🎁 🔑 **FastAPI**. FastAPI 🏆 🚫 ⚙️ ⚫️ 💽 🛠️, 🔬, ♒️. (⚫️ ⚙️ `= Depends(CommonQueryParams)` 👈).
👆 💪 🤙 ✍:
```Python
commons = Depends(CommonQueryParams)
```
...:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="19"
{!> ../../../docs_src/dependencies/tutorial003.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="17"
{!> ../../../docs_src/dependencies/tutorial003_py310.py!}
```
✋️ 📣 🆎 💡 👈 🌌 👆 👨‍🎨 🔜 💭 ⚫️❔ 🔜 🚶‍♀️ 🔢 `commons`, &amp; ⤴️ ⚫️ 💪 👆 ⏮️ 📟 🛠️, 🆎 ✅, ♒️:
<img src="/img/tutorial/dependencies/image02.png">
## ⌨
✋️ 👆 👀 👈 👥 ✔️ 📟 🔁 📥, ✍ `CommonQueryParams` 🕐:
```Python
commons: CommonQueryParams = Depends(CommonQueryParams)
```
**FastAPI** 🚚 ⌨ 👫 💼, 🌐❔ 🔗 *🎯* 🎓 👈 **FastAPI** 🔜 "🤙" ✍ 👐 🎓 ⚫️.
📚 🎯 💼, 👆 💪 📄:
↩️ ✍:
```Python
commons: CommonQueryParams = Depends(CommonQueryParams)
```
...👆 ✍:
```Python
commons: CommonQueryParams = Depends()
```
👆 📣 🔗 🆎 🔢, &amp; 👆 ⚙️ `Depends()` 🚮 "🔢" 💲 (👈 ⏮️ `=`) 👈 🔢 🔢, 🍵 🙆 🔢 `Depends()`, ↩️ ✔️ ✍ 🌕 🎓 *🔄* 🔘 `Depends(CommonQueryParams)`.
🎏 🖼 🔜 ⤴️ 👀 💖:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="19"
{!> ../../../docs_src/dependencies/tutorial004.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="17"
{!> ../../../docs_src/dependencies/tutorial004_py310.py!}
```
...&amp; **FastAPI** 🔜 💭 ⚫️❔.
!!! tip
🚥 👈 😑 🌅 😨 🌘 👍, 🤷‍♂ ⚫️, 👆 🚫 *💪* ⚫️.
⚫️ ⌨. ↩️ **FastAPI** 💅 🔃 🤝 👆 📉 📟 🔁.

View File

@@ -0,0 +1,71 @@
# 🔗 ➡ 🛠️ 👨‍🎨
💼 👆 🚫 🤙 💪 📨 💲 🔗 🔘 👆 *➡ 🛠️ 🔢*.
⚖️ 🔗 🚫 📨 💲.
✋️ 👆 💪 ⚫️ 🛠️/❎.
📚 💼, ↩️ 📣 *➡ 🛠️ 🔢* 🔢 ⏮️ `Depends`, 👆 💪 🚮 `list` `dependencies` *➡ 🛠️ 👨‍🎨*.
## 🚮 `dependencies` *➡ 🛠️ 👨‍🎨*
*➡ 🛠️ 👨‍🎨* 📨 📦 ❌ `dependencies`.
⚫️ 🔜 `list` `Depends()`:
```Python hl_lines="17"
{!../../../docs_src/dependencies/tutorial006.py!}
```
👉 🔗 🔜 🛠️/❎ 🎏 🌌 😐 🔗. ✋️ 👫 💲 (🚥 👫 📨 🙆) 🏆 🚫 🚶‍♀️ 👆 *➡ 🛠️ 🔢*.
!!! tip
👨‍🎨 ✅ ♻ 🔢 🔢, &amp; 🎦 👫 ❌.
⚙️ 👉 `dependencies` *➡ 🛠️ 👨‍🎨* 👆 💪 ⚒ 💭 👫 🛠️ ⏪ ❎ 👨‍🎨/🏭 ❌.
⚫️ 💪 ❎ 😨 🆕 👩‍💻 👈 👀 ♻ 🔢 👆 📟 &amp; 💪 💭 ⚫️ 🙃.
!!! info
👉 🖼 👥 ⚙️ 💭 🛃 🎚 `X-Key` &amp; `X-Token`.
✋️ 🎰 💼, 🕐❔ 🛠️ 💂‍♂, 👆 🔜 🤚 🌖 💰 ⚪️➡️ ⚙️ 🛠️ [💂‍♂ 🚙 (⏭ 📃)](../security/index.md){.internal-link target=_blank}.
## 🔗 ❌ &amp; 📨 💲
👆 💪 ⚙️ 🎏 🔗 *🔢* 👆 ⚙️ 🛎.
### 🔗 📄
👫 💪 📣 📨 📄 (💖 🎚) ⚖️ 🎏 🎧-🔗:
```Python hl_lines="6 11"
{!../../../docs_src/dependencies/tutorial006.py!}
```
### 🤚 ⚠
👫 🔗 💪 `raise` ⚠, 🎏 😐 🔗:
```Python hl_lines="8 13"
{!../../../docs_src/dependencies/tutorial006.py!}
```
### 📨 💲
&amp; 👫 💪 📨 💲 ⚖️ 🚫, 💲 🏆 🚫 ⚙️.
, 👆 💪 🏤-⚙️ 😐 🔗 (👈 📨 💲) 👆 ⏪ ⚙️ 👱 🙆, &amp; ✋️ 💲 🏆 🚫 ⚙️, 🔗 🔜 🛠️:
```Python hl_lines="9 14"
{!../../../docs_src/dependencies/tutorial006.py!}
```
## 🔗 👪 *➡ 🛠️*
⏪, 🕐❔ 👂 🔃 ❔ 📊 🦏 🈸 ([🦏 🈸 - 💗 📁](../../tutorial/bigger-applications.md){.internal-link target=_blank}), 🎲 ⏮️ 💗 📁, 👆 🔜 💡 ❔ 📣 👁 `dependencies` 🔢 👪 *➡ 🛠️*.
## 🌐 🔗
⏭ 👥 🔜 👀 ❔ 🚮 🔗 🎂 `FastAPI` 🈸, 👈 👫 ✔ 🔠 *➡ 🛠️*.

View File

@@ -0,0 +1,219 @@
# 🔗 ⏮️ 🌾
FastAPI 🐕‍🦺 🔗 👈 <abbr title='sometimes also called "exit", "cleanup", "teardown", "close", "context managers", ...'> 🔁 ⏮️ 🏁</abbr>.
👉, ⚙️ `yield` ↩️ `return`, &amp; ✍ 🔁 ⏮️.
!!! tip
⚒ 💭 ⚙️ `yield` 1⃣ 👁 🕰.
!!! note "📡 "
🙆 🔢 👈 ☑ ⚙️ ⏮️:
* <a href="https://docs.python.org/3/library/contextlib.html#contextlib.contextmanager" class="external-link" target="_blank">`@contextlib.contextmanager`</a> ⚖️
* <a href="https://docs.python.org/3/library/contextlib.html#contextlib.asynccontextmanager" class="external-link" target="_blank">`@contextlib.asynccontextmanager`</a>
🔜 ☑ ⚙️ **FastAPI** 🔗.
👐, FastAPI ⚙️ 📚 2⃣ 👨‍🎨 🔘.
## 💽 🔗 ⏮️ `yield`
🖼, 👆 💪 ⚙️ 👉 ✍ 💽 🎉 &amp; 🔐 ⚫️ ⏮️ 🏁.
🕴 📟 ⏭ &amp; 🔌 `yield` 📄 🛠️ ⏭ 📨 📨:
```Python hl_lines="2-4"
{!../../../docs_src/dependencies/tutorial007.py!}
```
🌾 💲 ⚫️❔ 💉 🔘 *➡ 🛠️* &amp; 🎏 🔗:
```Python hl_lines="4"
{!../../../docs_src/dependencies/tutorial007.py!}
```
📟 📄 `yield` 📄 🛠️ ⏮️ 📨 ✔️ 🚚:
```Python hl_lines="5-6"
{!../../../docs_src/dependencies/tutorial007.py!}
```
!!! tip
👆 💪 ⚙️ `async` ⚖️ 😐 🔢.
**FastAPI** 🔜 ▶️️ 👜 ⏮️ 🔠, 🎏 ⏮️ 😐 🔗.
## 🔗 ⏮️ `yield` &amp; `try`
🚥 👆 ⚙️ `try` 🍫 🔗 ⏮️ `yield`, 👆 🔜 📨 🙆 ⚠ 👈 🚮 🕐❔ ⚙️ 🔗.
🖼, 🚥 📟 ☝ 🖕, 1⃣ 🔗 ⚖️ *➡ 🛠️*, ⚒ 💽 💵 "💾" ⚖️ ✍ 🙆 🎏 ❌, 👆 🔜 📨 ⚠ 👆 🔗.
, 👆 💪 👀 👈 🎯 ⚠ 🔘 🔗 ⏮️ `except SomeException`.
🎏 🌌, 👆 💪 ⚙️ `finally` ⚒ 💭 🚪 📶 🛠️, 🙅‍♂ 🤔 🚥 📤 ⚠ ⚖️ 🚫.
```Python hl_lines="3 5"
{!../../../docs_src/dependencies/tutorial007.py!}
```
## 🎧-🔗 ⏮️ `yield`
👆 💪 ✔️ 🎧-🔗 &amp; "🌲" 🎧-🔗 🙆 📐 &amp; 💠, &amp; 🙆 ⚖️ 🌐 👫 💪 ⚙️ `yield`.
**FastAPI** 🔜 ⚒ 💭 👈 "🚪 📟" 🔠 🔗 ⏮️ `yield` 🏃 ☑ ✔.
🖼, `dependency_c` 💪 ✔️ 🔗 🔛 `dependency_b`, &amp; `dependency_b` 🔛 `dependency_a`:
```Python hl_lines="4 12 20"
{!../../../docs_src/dependencies/tutorial008.py!}
```
&amp; 🌐 👫 💪 ⚙️ `yield`.
👉 💼 `dependency_c`, 🛠️ 🚮 🚪 📟, 💪 💲 ⚪️➡️ `dependency_b` (📥 📛 `dep_b`) 💪.
&amp; , 🔄, `dependency_b` 💪 💲 ⚪️➡️ `dependency_a` (📥 📛 `dep_a`) 💪 🚮 🚪 📟.
```Python hl_lines="16-17 24-25"
{!../../../docs_src/dependencies/tutorial008.py!}
```
🎏 🌌, 👆 💪 ✔️ 🔗 ⏮️ `yield` &amp; `return` 🌀.
&amp; 👆 💪 ✔️ 👁 🔗 👈 🚚 📚 🎏 🔗 ⏮️ `yield`, ♒️.
👆 💪 ✔️ 🙆 🌀 🔗 👈 👆 💚.
**FastAPI** 🔜 ⚒ 💭 🌐 🏃 ☑ ✔.
!!! note "📡 "
👉 👷 👏 🐍 <a href="https://docs.python.org/3/library/contextlib.html" class="external-link" target="_blank">🔑 👨‍💼</a>.
**FastAPI** ⚙️ 👫 🔘 🏆 👉.
## 🔗 ⏮️ `yield` &amp; `HTTPException`
👆 👀 👈 👆 💪 ⚙️ 🔗 ⏮️ `yield` &amp; ✔️ `try` 🍫 👈 ✊ ⚠.
⚫️ 5⃣📆 😋 🤚 `HTTPException` ⚖️ 🎏 🚪 📟, ⏮️ `yield`. ✋️ **⚫️ 🏆 🚫 👷**.
🚪 📟 🔗 ⏮️ `yield` 🛠️ *⏮️* 📨 📨, [⚠ 🐕‍🦺](../handling-errors.md#install-custom-exception-handlers){.internal-link target=_blank} 🔜 ✔️ ⏪ 🏃. 📤 🕳 😽 ⚠ 🚮 👆 🔗 🚪 📟 (⏮️ `yield`).
, 🚥 👆 🤚 `HTTPException` ⏮️ `yield`, 🔢 (⚖️ 🙆 🛃) ⚠ 🐕‍🦺 👈 ✊ `HTTPException`Ⓜ &amp; 📨 🇺🇸🔍 4⃣0⃣0⃣ 📨 🏆 🚫 📤 ✊ 👈 ⚠ 🚫🔜.
👉 ⚫️❔ ✔ 🕳 ⚒ 🔗 (✅ 💽 🎉), 🖼, ⚙️ 🖥 📋.
🖥 📋 🏃 *⏮️* 📨 ✔️ 📨. 📤 🙅‍♂ 🌌 🤚 `HTTPException` ↩️ 📤 🚫 🌌 🔀 📨 👈 *⏪ 📨*.
✋️ 🚥 🖥 📋 ✍ 💽 ❌, 🌘 👆 💪 💾 ⚖️ 😬 🔐 🎉 🔗 ⏮️ `yield`, &amp; 🎲 🕹 ❌ ⚖️ 📄 ⚫️ 🛰 🕵 ⚙️.
🚥 👆 ✔️ 📟 👈 👆 💭 💪 🤚 ⚠, 🏆 😐/"🙃" 👜 &amp; 🚮 `try` 🍫 👈 📄 📟.
🚥 👆 ✔️ 🛃 ⚠ 👈 👆 🔜 💖 🍵 *⏭* 🛬 📨 &amp; 🎲 ❎ 📨, 🎲 🙋‍♀ `HTTPException`, ✍ [🛃 ⚠ 🐕‍🦺](../handling-errors.md#install-custom-exception-handlers){.internal-link target=_blank}.
!!! tip
👆 💪 🤚 ⚠ 🔌 `HTTPException` *⏭* `yield`. ✋️ 🚫 ⏮️.
🔁 🛠️ 🌅 ⚖️ 🌘 💖 👉 📊. 🕰 💧 ⚪️➡️ 🔝 🔝. &amp; 🔠 🏓 1⃣ 🍕 🔗 ⚖️ 🛠️ 📟.
```mermaid
sequenceDiagram
participant client as Client
participant handler as Exception handler
participant dep as Dep with yield
participant operation as Path Operation
participant tasks as Background tasks
Note over client,tasks: Can raise exception for dependency, handled after response is sent
Note over client,operation: Can raise HTTPException and can change the response
client ->> dep: Start request
Note over dep: Run code up to yield
opt raise
dep -->> handler: Raise HTTPException
handler -->> client: HTTP error response
dep -->> dep: Raise other exception
end
dep ->> operation: Run dependency, e.g. DB session
opt raise
operation -->> dep: Raise HTTPException
dep -->> handler: Auto forward exception
handler -->> client: HTTP error response
operation -->> dep: Raise other exception
dep -->> handler: Auto forward exception
end
operation ->> client: Return response to client
Note over client,operation: Response is already sent, can't change it anymore
opt Tasks
operation -->> tasks: Send background tasks
end
opt Raise other exception
tasks -->> dep: Raise other exception
end
Note over dep: After yield
opt Handle other exception
dep -->> dep: Handle exception, can't change response. E.g. close DB session.
end
```
!!! info
🕴 **1⃣ 📨** 🔜 📨 👩‍💻. ⚫️ 💪 1⃣ ❌ 📨 ⚖️ ⚫️ 🔜 📨 ⚪️➡️ *➡ 🛠️*.
⏮️ 1⃣ 📚 📨 📨, 🙅‍♂ 🎏 📨 💪 📨.
!!! tip
👉 📊 🎦 `HTTPException`, ✋️ 👆 💪 🤚 🙆 🎏 ⚠ ❔ 👆 ✍ [🛃 ⚠ 🐕‍🦺](../handling-errors.md#install-custom-exception-handlers){.internal-link target=_blank}.
🚥 👆 🤚 🙆 ⚠, ⚫️ 🔜 🚶‍♀️ 🔗 ⏮️ 🌾, 🔌 `HTTPException`, &amp; ⤴️ **🔄** ⚠ 🐕‍🦺. 🚥 📤 🙅‍♂ ⚠ 🐕‍🦺 👈 ⚠, ⚫️ 🔜 ⤴️ 🍵 🔢 🔗 `ServerErrorMiddleware`, 🛬 5⃣0⃣0⃣ 🇺🇸🔍 👔 📟, ➡️ 👩‍💻 💭 👈 📤 ❌ 💽.
## 🔑 👨‍💼
### ⚫️❔ "🔑 👨‍💼"
"🔑 👨‍💼" 🙆 👈 🐍 🎚 👈 👆 💪 ⚙️ `with` 📄.
🖼, <a href="https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files" class="external-link" target="_blank">👆 💪 ⚙️ `with` ✍ 📁</a>:
```Python
with open("./somefile.txt") as f:
contents = f.read()
print(contents)
```
🔘, `open("./somefile.txt")` ✍ 🎚 👈 🤙 "🔑 👨‍💼".
🕐❔ `with` 🍫 🏁, ⚫️ ⚒ 💭 🔐 📁, 🚥 📤 ⚠.
🕐❔ 👆 ✍ 🔗 ⏮️ `yield`, **FastAPI** 🔜 🔘 🗜 ⚫️ 🔑 👨‍💼, &amp; 🌀 ⚫️ ⏮️ 🎏 🔗 🧰.
### ⚙️ 🔑 👨‍💼 🔗 ⏮️ `yield`
!!! warning
👉, 🌅 ⚖️ 🌘, "🏧" 💭.
🚥 👆 ▶️ ⏮️ **FastAPI** 👆 💪 💚 🚶 ⚫️ 🔜.
🐍, 👆 💪 ✍ 🔑 👨‍💼 <a href="https://docs.python.org/3/reference/datamodel.html#context-managers" class="external-link" target="_blank">🏗 🎓 ⏮️ 2⃣ 👩‍🔬: `__enter__()` &amp; `__exit__()`</a>.
👆 💪 ⚙️ 👫 🔘 **FastAPI** 🔗 ⏮️ `yield` ⚙️
`with` ⚖️ `async with` 📄 🔘 🔗 🔢:
```Python hl_lines="1-9 13"
{!../../../docs_src/dependencies/tutorial010.py!}
```
!!! tip
1⃣ 🌌 ✍ 🔑 👨‍💼 ⏮️:
* <a href="https://docs.python.org/3/library/contextlib.html#contextlib.contextmanager" class="external-link" target="_blank">`@contextlib.contextmanager`</a> ⚖️
* <a href="https://docs.python.org/3/library/contextlib.html#contextlib.asynccontextmanager" class="external-link" target="_blank">`@contextlib.asynccontextmanager`</a>
⚙️ 👫 🎀 🔢 ⏮️ 👁 `yield`.
👈 ⚫️❔ **FastAPI** ⚙️ 🔘 🔗 ⏮️ `yield`.
✋️ 👆 🚫 ✔️ ⚙️ 👨‍🎨 FastAPI 🔗 (&amp; 👆 🚫🔜 🚫).
FastAPI 🔜 ⚫️ 👆 🔘.

View File

@@ -0,0 +1,17 @@
# 🌐 🔗
🆎 🈸 👆 💪 💚 🚮 🔗 🎂 🈸.
🎏 🌌 👆 💪 [🚮 `dependencies` *➡ 🛠️ 👨‍🎨*](dependencies-in-path-operation-decorators.md){.internal-link target=_blank}, 👆 💪 🚮 👫 `FastAPI` 🈸.
👈 💼, 👫 🔜 ✔ 🌐 *➡ 🛠️* 🈸:
```Python hl_lines="15"
{!../../../docs_src/dependencies/tutorial012.py!}
```
&amp; 🌐 💭 📄 🔃 [❎ `dependencies` *➡ 🛠️ 👨‍🎨*](dependencies-in-path-operation-decorators.md){.internal-link target=_blank} ✔, ✋️ 👉 💼, 🌐 *➡ 🛠️* 📱.
## 🔗 👪 *➡ 🛠️*
⏪, 🕐❔ 👂 🔃 ❔ 📊 🦏 🈸 ([🦏 🈸 - 💗 📁](../../tutorial/bigger-applications.md){.internal-link target=_blank}), 🎲 ⏮️ 💗 📁, 👆 🔜 💡 ❔ 📣 👁 `dependencies` 🔢 👪 *➡ 🛠️*.

View File

@@ -0,0 +1,233 @@
# 🔗 - 🥇 🔁
**FastAPI** ✔️ 📶 🏋️ ✋️ 🏋️ **<abbr title="also known as components, resources, providers, services, injectables">🔗 💉</abbr>** ⚙️.
⚫️ 🏗 📶 🙅 ⚙️, &amp; ⚒ ⚫️ 📶 ⏩ 🙆 👩‍💻 🛠️ 🎏 🦲 ⏮️ **FastAPI**.
## ⚫️❔ "🔗 💉"
**"🔗 💉"** ⛓, 📋, 👈 📤 🌌 👆 📟 (👉 💼, 👆 *➡ 🛠️ 🔢*) 📣 👜 👈 ⚫️ 🚚 👷 &amp; ⚙️: "🔗".
&amp; ⤴️, 👈 ⚙️ (👉 💼 **FastAPI**) 🔜 ✊ 💅 🔨 ⚫️❔ 💪 🚚 👆 📟 ⏮️ 📚 💪 🔗 ("💉" 🔗).
👉 📶 ⚠ 🕐❔ 👆 💪:
* ✔️ 💰 ⚛ (🎏 📟 ⚛ 🔄 &amp; 🔄).
* 💰 💽 🔗.
* 🛠️ 💂‍♂, 🤝, 🔑 📄, ♒️.
* &amp; 📚 🎏 👜...
🌐 👫, ⏪ 📉 📟 🔁.
## 🥇 🔁
➡️ 👀 📶 🙅 🖼. ⚫️ 🔜 🙅 👈 ⚫️ 🚫 📶 ⚠, 🔜.
✋️ 👉 🌌 👥 💪 🎯 🔛 ❔ **🔗 💉** ⚙️ 👷.
### ✍ 🔗, ⚖️ "☑"
➡️ 🥇 🎯 🔛 🔗.
⚫️ 🔢 👈 💪 ✊ 🌐 🎏 🔢 👈 *➡ 🛠️ 🔢* 💪 ✊:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="8-11"
{!> ../../../docs_src/dependencies/tutorial001.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="6-7"
{!> ../../../docs_src/dependencies/tutorial001_py310.py!}
```
👈 ⚫️.
**2⃣ ⏸**.
&amp; ⚫️ ✔️ 🎏 💠 &amp; 📊 👈 🌐 👆 *➡ 🛠️ 🔢* ✔️.
👆 💪 💭 ⚫️ *➡ 🛠️ 🔢* 🍵 "👨‍🎨" (🍵 `@app.get("/some-path")`).
&amp; ⚫️ 💪 📨 🕳 👆 💚.
👉 💼, 👉 🔗 ⌛:
* 📦 🔢 🔢 `q` 👈 `str`.
* 📦 🔢 🔢 `skip` 👈 `int`, &amp; 🔢 `0`.
* 📦 🔢 🔢 `limit` 👈 `int`, &amp; 🔢 `100`.
&amp; ⤴️ ⚫️ 📨 `dict` ⚗ 📚 💲.
### 🗄 `Depends`
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="3"
{!> ../../../docs_src/dependencies/tutorial001.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="1"
{!> ../../../docs_src/dependencies/tutorial001_py310.py!}
```
### 📣 🔗, "⚓️"
🎏 🌌 👆 ⚙️ `Body`, `Query`, ♒️. ⏮️ 👆 *➡ 🛠️ 🔢* 🔢, ⚙️ `Depends` ⏮️ 🆕 🔢:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="15 20"
{!> ../../../docs_src/dependencies/tutorial001.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="11 16"
{!> ../../../docs_src/dependencies/tutorial001_py310.py!}
```
👐 👆 ⚙️ `Depends` 🔢 👆 🔢 🎏 🌌 👆 ⚙️ `Body`, `Query`, ♒️, `Depends` 👷 👄 🎏.
👆 🕴 🤝 `Depends` 👁 🔢.
👉 🔢 🔜 🕳 💖 🔢.
&amp; 👈 🔢 ✊ 🔢 🎏 🌌 👈 *➡ 🛠️ 🔢* .
!!! tip
👆 🔜 👀 ⚫️❔ 🎏 "👜", ↖️ ⚪️➡️ 🔢, 💪 ⚙️ 🔗 ⏭ 📃.
🕐❔ 🆕 📨 🛬, **FastAPI** 🔜 ✊ 💅:
* 🤙 👆 🔗 ("☑") 🔢 ⏮️ ☑ 🔢.
* 🤚 🏁 ⚪️➡️ 👆 🔢.
* 🛠️ 👈 🏁 🔢 👆 *➡ 🛠️ 🔢*.
```mermaid
graph TB
common_parameters(["common_parameters"])
read_items["/items/"]
read_users["/users/"]
common_parameters --> read_items
common_parameters --> read_users
```
👉 🌌 👆 ✍ 🔗 📟 🕐 &amp; **FastAPI** ✊ 💅 🤙 ⚫️ 👆 *➡ 🛠️*.
!!! check
👀 👈 👆 🚫 ✔️ ✍ 🎁 🎓 &amp; 🚶‍♀️ ⚫️ 👱 **FastAPI** "®" ⚫️ ⚖️ 🕳 🎏.
👆 🚶‍♀️ ⚫️ `Depends` &amp; **FastAPI** 💭 ❔ 🎂.
## `async` ⚖️ 🚫 `async`
🔗 🔜 🤙 **FastAPI** (🎏 👆 *➡ 🛠️ 🔢*), 🎏 🚫 ✔ ⏪ 🔬 👆 🔢.
👆 💪 ⚙️ `async def` ⚖️ 😐 `def`.
&amp; 👆 💪 📣 🔗 ⏮️ `async def` 🔘 😐 `def` *➡ 🛠️ 🔢*, ⚖️ `def` 🔗 🔘 `async def` *➡ 🛠️ 🔢*, ♒️.
⚫️ 🚫 🤔. **FastAPI** 🔜 💭 ⚫️❔.
!!! note
🚥 👆 🚫 💭, ✅ [🔁: *"🏃 ❓" *](../../async.md){.internal-link target=_blank} 📄 🔃 `async` &amp; `await` 🩺.
## 🛠️ ⏮️ 🗄
🌐 📨 📄, 🔬 &amp; 📄 👆 🔗 (&amp; 🎧-🔗) 🔜 🛠️ 🎏 🗄 🔗.
, 🎓 🩺 🔜 ✔️ 🌐 ⚪️➡️ 👫 🔗 💁‍♂️:
<img src="/img/tutorial/dependencies/image01.png">
## 🙅 ⚙️
🚥 👆 👀 ⚫️, *➡ 🛠️ 🔢* 📣 ⚙️ 🕐❔ *➡* &amp; *🛠️* 🏏, &amp; ⤴️ **FastAPI** ✊ 💅 🤙 🔢 ⏮️ ☑ 🔢, ❎ 📊 ⚪️➡️ 📨.
🤙, 🌐 (⚖️ 🏆) 🕸 🛠️ 👷 👉 🎏 🌌.
👆 🙅 🤙 👈 🔢 🔗. 👫 🤙 👆 🛠️ (👉 💼, **FastAPI**).
⏮️ 🔗 💉 ⚙️, 👆 💪 💬 **FastAPI** 👈 👆 *➡ 🛠️ 🔢* "🪀" 🔛 🕳 🙆 👈 🔜 🛠️ ⏭ 👆 *➡ 🛠️ 🔢*, &amp; **FastAPI** 🔜 ✊ 💅 🛠️ ⚫️ &amp; "💉" 🏁.
🎏 ⚠ ⚖ 👉 🎏 💭 "🔗 💉":
*
* 🐕‍🦺
* 🐕‍🦺
* 💉
* 🦲
## **FastAPI** 🔌-🔌
🛠️ &amp; "🔌-"Ⓜ 💪 🏗 ⚙️ **🔗 💉** ⚙️. ✋️ 👐, 📤 🤙 **🙅‍♂ 💪 ✍ "🔌-🔌"**, ⚙️ 🔗 ⚫️ 💪 📣 ♾ 🔢 🛠️ &amp; 🔗 👈 ▶️️ 💪 👆 *➡ 🛠️ 🔢*.
&amp; 🔗 💪 ✍ 📶 🙅 &amp; 🏋️ 🌌 👈 ✔ 👆 🗄 🐍 📦 👆 💪, &amp; 🛠️ 👫 ⏮️ 👆 🛠️ 🔢 👩‍❤‍👨 ⏸ 📟, *🌖*.
👆 🔜 👀 🖼 👉 ⏭ 📃, 🔃 🔗 &amp; ☁ 💽, 💂‍♂, ♒️.
## **FastAPI** 🔗
🦁 🔗 💉 ⚙️ ⚒ **FastAPI** 🔗 ⏮️:
* 🌐 🔗 💽
* ☁ 💽
* 🔢 📦
* 🔢 🔗
* 🤝 &amp; ✔ ⚙️
* 🛠️ ⚙️ ⚖ ⚙️
* 📨 💽 💉 ⚙️
* ♒️.
## 🙅 &amp; 🏋️
👐 🔗 🔗 💉 ⚙️ 📶 🙅 🔬 &amp; ⚙️, ⚫️ 📶 🏋️.
👆 💪 🔬 🔗 👈 🔄 💪 🔬 🔗 👫.
🔚, 🔗 🌲 🔗 🏗, &amp; **🔗 💉** ⚙️ ✊ 💅 🔬 🌐 👉 🔗 👆 (&amp; 👫 🎧-🔗) &amp; 🚚 (💉) 🏁 🔠 🔁.
🖼, ➡️ 💬 👆 ✔️ 4⃣ 🛠️ 🔗 (*➡ 🛠️*):
* `/items/public/`
* `/items/private/`
* `/users/{user_id}/activate`
* `/items/pro/`
⤴️ 👆 💪 🚮 🎏 ✔ 📄 🔠 👫 ⏮️ 🔗 &amp; 🎧-🔗:
```mermaid
graph TB
current_user(["current_user"])
active_user(["active_user"])
admin_user(["admin_user"])
paying_user(["paying_user"])
public["/items/public/"]
private["/items/private/"]
activate_user["/users/{user_id}/activate"]
pro_items["/items/pro/"]
current_user --> active_user
active_user --> admin_user
active_user --> paying_user
current_user --> public
active_user --> private
admin_user --> activate_user
paying_user --> pro_items
```
## 🛠️ ⏮️ **🗄**
🌐 👫 🔗, ⏪ 📣 👫 📄, 🚮 🔢, 🔬, ♒️. 👆 *➡ 🛠️*.
**FastAPI** 🔜 ✊ 💅 🚮 ⚫️ 🌐 🗄 🔗, 👈 ⚫️ 🎦 🎓 🧾 ⚙️.

View File

@@ -0,0 +1,110 @@
# 🎧-🔗
👆 💪 ✍ 🔗 👈 ✔️ **🎧-🔗**.
👫 💪 **⏬** 👆 💪 👫.
**FastAPI** 🔜 ✊ 💅 🔬 👫.
## 🥇 🔗 "☑"
👆 💪 ✍ 🥇 🔗 ("☑") 💖:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="8-9"
{!> ../../../docs_src/dependencies/tutorial005.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="6-7"
{!> ../../../docs_src/dependencies/tutorial005_py310.py!}
```
⚫️ 📣 📦 🔢 🔢 `q` `str`, &amp; ⤴️ ⚫️ 📨 ⚫️.
👉 🙅 (🚫 📶 ⚠), ✋️ 🔜 👥 🎯 🔛 ❔ 🎧-🔗 👷.
## 🥈 🔗, "☑" &amp; "⚓️"
⤴️ 👆 💪 ✍ 1⃣ 🔗 🔢 ("☑") 👈 🎏 🕰 📣 🔗 🚮 👍 (⚫️ "⚓️" 💁‍♂️):
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="13"
{!> ../../../docs_src/dependencies/tutorial005.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="11"
{!> ../../../docs_src/dependencies/tutorial005_py310.py!}
```
➡️ 🎯 🔛 🔢 📣:
* ✋️ 👉 🔢 🔗 ("☑") ⚫️, ⚫️ 📣 1⃣ 🔗 (⚫️ "🪀" 🔛 🕳 🙆).
* ⚫️ 🪀 🔛 `query_extractor`, &amp; 🛠️ 💲 📨 ⚫️ 🔢 `q`.
* ⚫️ 📣 📦 `last_query` 🍪, `str`.
* 🚥 👩‍💻 🚫 🚚 🙆 🔢 `q`, 👥 ⚙️ 🏁 🔢 ⚙️, ❔ 👥 🖊 🍪 ⏭.
## ⚙️ 🔗
⤴️ 👥 💪 ⚙️ 🔗 ⏮️:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="22"
{!> ../../../docs_src/dependencies/tutorial005.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="19"
{!> ../../../docs_src/dependencies/tutorial005_py310.py!}
```
!!! info
👀 👈 👥 🕴 📣 1⃣ 🔗 *➡ 🛠️ 🔢*, `query_or_cookie_extractor`.
✋️ **FastAPI** 🔜 💭 👈 ⚫️ ✔️ ❎ `query_extractor` 🥇, 🚶‍♀️ 🏁 👈 `query_or_cookie_extractor` ⏪ 🤙 ⚫️.
```mermaid
graph TB
query_extractor(["query_extractor"])
query_or_cookie_extractor(["query_or_cookie_extractor"])
read_query["/items/"]
query_extractor --> query_or_cookie_extractor --> read_query
```
## ⚙️ 🎏 🔗 💗 🕰
🚥 1⃣ 👆 🔗 📣 💗 🕰 🎏 *➡ 🛠️*, 🖼, 💗 🔗 ✔️ ⚠ 🎧-🔗, **FastAPI** 🔜 💭 🤙 👈 🎧-🔗 🕴 🕐 📍 📨.
&amp; ⚫️ 🔜 🖊 📨 💲 <abbr title="A utility/system to store computed/generated values, to re-use them instead of computing them again.">"💾"</abbr> &amp; 🚶‍♀️ ⚫️ 🌐 "⚓️" 👈 💪 ⚫️ 👈 🎯 📨, ↩️ 🤙 🔗 💗 🕰 🎏 📨.
🏧 😐 🌐❔ 👆 💭 👆 💪 🔗 🤙 🔠 🔁 (🎲 💗 🕰) 🎏 📨 ↩️ ⚙️ "💾" 💲, 👆 💪 ⚒ 🔢 `use_cache=False` 🕐❔ ⚙️ `Depends`:
```Python hl_lines="1"
async def needy_dependency(fresh_value: str = Depends(get_value, use_cache=False)):
return {"fresh_value": fresh_value}
```
## 🌃
↖️ ⚪️➡️ 🌐 🎀 🔤 ⚙️ 📥, **🔗 💉** ⚙️ 🙅.
🔢 👈 👀 🎏 *➡ 🛠️ 🔢*.
✋️, ⚫️ 📶 🏋️, &amp; ✔ 👆 📣 🎲 🙇 🐦 🔗 "📊" (🌲).
!!! tip
🌐 👉 💪 🚫 😑 ⚠ ⏮️ 👫 🙅 🖼.
✋️ 👆 🔜 👀 ❔ ⚠ ⚫️ 📃 🔃 **💂‍♂**.
&amp; 👆 🔜 👀 💸 📟 ⚫️ 🔜 🖊 👆.

View File

@@ -0,0 +1,42 @@
# 🎻 🔗 🔢
📤 💼 🌐❔ 👆 5⃣📆 💪 🗜 💽 🆎 (💖 Pydantic 🏷) 🕳 🔗 ⏮️ 🎻 (💖 `dict`, `list`, ♒️).
🖼, 🚥 👆 💪 🏪 ⚫️ 💽.
👈, **FastAPI** 🚚 `jsonable_encoder()` 🔢.
## ⚙️ `jsonable_encoder`
➡️ 🌈 👈 👆 ✔️ 💽 `fake_db` 👈 🕴 📨 🎻 🔗 💽.
🖼, ⚫️ 🚫 📨 `datetime` 🎚, 👈 🚫 🔗 ⏮️ 🎻.
, `datetime` 🎚 🔜 ✔️ 🗜 `str` ⚗ 💽 <a href="https://en.wikipedia.org/wiki/ISO_8601" class="external-link" target="_blank">💾 📁</a>.
🎏 🌌, 👉 💽 🚫🔜 📨 Pydantic 🏷 (🎚 ⏮️ 🔢), 🕴 `dict`.
👆 💪 ⚙️ `jsonable_encoder` 👈.
⚫️ 📨 🎚, 💖 Pydantic 🏷, &amp; 📨 🎻 🔗 ⏬:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="5 22"
{!> ../../../docs_src/encoder/tutorial001.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="4 21"
{!> ../../../docs_src/encoder/tutorial001_py310.py!}
```
👉 🖼, ⚫️ 🔜 🗜 Pydantic 🏷 `dict`, &amp; `datetime` `str`.
🏁 🤙 ⚫️ 🕳 👈 💪 🗜 ⏮️ 🐍 🐩 <a href="https://docs.python.org/3/library/json.html#json.dumps" class="external-link" target="_blank">`json.dumps()`</a>.
⚫️ 🚫 📨 ⭕ `str` ⚗ 💽 🎻 📁 (🎻). ⚫️ 📨 🐍 🐩 💽 📊 (✅ `dict`) ⏮️ 💲 &amp; 🎧-💲 👈 🌐 🔗 ⏮️ 🎻.
!!! note
`jsonable_encoder` 🤙 ⚙️ **FastAPI** 🔘 🗜 💽. ✋️ ⚫️ ⚠ 📚 🎏 😐.

View File

@@ -0,0 +1,82 @@
# 💽 🆎
🆙 🔜, 👆 ✔️ ⚙️ ⚠ 📊 🆎, 💖:
* `int`
* `float`
* `str`
* `bool`
✋️ 👆 💪 ⚙️ 🌅 🏗 📊 🆎.
&amp; 👆 🔜 ✔️ 🎏 ⚒ 👀 🆙 🔜:
* 👑 👨‍🎨 🐕‍🦺.
* 💽 🛠️ ⚪️➡️ 📨 📨.
* 💽 🛠️ 📨 💽.
* 💽 🔬.
* 🏧 ✍ &amp; 🧾.
## 🎏 💽 🆎
📥 🌖 📊 🆎 👆 💪 ⚙️:
* `UUID`:
* 🐩 "⭐ 😍 🆔", ⚠ 🆔 📚 💽 &amp; ⚙️.
* 📨 &amp; 📨 🔜 🎨 `str`.
* `datetime.datetime`:
* 🐍 `datetime.datetime`.
* 📨 &amp; 📨 🔜 🎨 `str` 💾 8⃣6⃣0⃣1⃣ 📁, 💖: `2008-09-15T15:53:00+05:00`.
* `datetime.date`:
* 🐍 `datetime.date`.
* 📨 &amp; 📨 🔜 🎨 `str` 💾 8⃣6⃣0⃣1⃣ 📁, 💖: `2008-09-15`.
* `datetime.time`:
* 🐍 `datetime.time`.
* 📨 &amp; 📨 🔜 🎨 `str` 💾 8⃣6⃣0⃣1⃣ 📁, 💖: `14:23:55.003`.
* `datetime.timedelta`:
* 🐍 `datetime.timedelta`.
* 📨 &amp; 📨 🔜 🎨 `float` 🌐 🥈.
* Pydantic ✔ 🎦 ⚫️ "💾 8⃣6⃣0⃣1⃣ 🕰 🔢", <a href="https://pydantic-docs.helpmanual.io/usage/exporting_models/#json_encoders" class="external-link" target="_blank">👀 🩺 🌅 </a>.
* `frozenset`:
* 📨 &amp; 📨, 😥 🎏 `set`:
* 📨, 📇 🔜 ✍, ❎ ❎ &amp; 🏭 ⚫️ `set`.
* 📨, `set` 🔜 🗜 `list`.
* 🏗 🔗 🔜 ✔ 👈 `set` 💲 😍 (⚙️ 🎻 🔗 `uniqueItems`).
* `bytes`:
* 🐩 🐍 `bytes`.
* 📨 &amp; 📨 🔜 😥 `str`.
* 🏗 🔗 🔜 ✔ 👈 ⚫️ `str` ⏮️ `binary` "📁".
* `Decimal`:
* 🐩 🐍 `Decimal`.
* 📨 &amp; 📨, 🍵 🎏 `float`.
* 👆 💪 ✅ 🌐 ☑ Pydantic 📊 🆎 📥: <a href="https://pydantic-docs.helpmanual.io/usage/types" class="external-link" target="_blank">Pydantic 📊 🆎</a>.
## 🖼
📥 🖼 *➡ 🛠️* ⏮️ 🔢 ⚙️ 🔛 🆎.
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="1 3 12-16"
{!> ../../../docs_src/extra_data_types/tutorial001.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="1 2 11-15"
{!> ../../../docs_src/extra_data_types/tutorial001_py310.py!}
```
🗒 👈 🔢 🔘 🔢 ✔️ 👫 🐠 💽 🆎, &amp; 👆 💪, 🖼, 🎭 😐 📅 🎭, 💖:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="18-19"
{!> ../../../docs_src/extra_data_types/tutorial001.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="17-18"
{!> ../../../docs_src/extra_data_types/tutorial001_py310.py!}
```

View File

@@ -0,0 +1,252 @@
# 🏷
▶️ ⏮️ ⏮️ 🖼, ⚫️ 🔜 ⚠ ✔️ 🌅 🌘 1⃣ 🔗 🏷.
👉 ✴️ 💼 👩‍💻 🏷, ↩️:
* **🔢 🏷** 💪 💪 ✔️ 🔐.
* **🔢 🏷** 🔜 🚫 ✔️ 🔐.
* **💽 🏷** 🔜 🎲 💪 ✔️ #⃣ 🔐.
!!! danger
🙅 🏪 👩‍💻 🔢 🔐. 🕧 🏪 "🔐 #⃣" 👈 👆 💪 ⤴️ ✔.
🚥 👆 🚫 💭, 👆 🔜 💡 ⚫️❔ "🔐#⃣" [💂‍♂ 📃](security/simple-oauth2.md#password-hashing){.internal-link target=_blank}.
## 💗 🏷
📥 🏢 💭 ❔ 🏷 💪 👀 💖 ⏮️ 👫 🔐 🏑 &amp; 🥉 🌐❔ 👫 ⚙️:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="9 11 16 22 24 29-30 33-35 40-41"
{!> ../../../docs_src/extra_models/tutorial001.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="7 9 14 20 22 27-28 31-33 38-39"
{!> ../../../docs_src/extra_models/tutorial001_py310.py!}
```
### 🔃 `**user_in.dict()`
#### Pydantic `.dict()`
`user_in` Pydantic 🏷 🎓 `UserIn`.
Pydantic 🏷 ✔️ `.dict()` 👩‍🔬 👈 📨 `dict` ⏮️ 🏷 💽.
, 🚥 👥 ✍ Pydantic 🎚 `user_in` 💖:
```Python
user_in = UserIn(username="john", password="secret", email="john.doe@example.com")
```
&amp; ⤴️ 👥 🤙:
```Python
user_dict = user_in.dict()
```
👥 🔜 ✔️ `dict` ⏮️ 💽 🔢 `user_dict` (⚫️ `dict` ↩️ Pydantic 🏷 🎚).
&amp; 🚥 👥 🤙:
```Python
print(user_dict)
```
👥 🔜 🤚 🐍 `dict` ⏮️:
```Python
{
'username': 'john',
'password': 'secret',
'email': 'john.doe@example.com',
'full_name': None,
}
```
#### 🎁 `dict`
🚥 👥 ✊ `dict` 💖 `user_dict` &amp; 🚶‍♀️ ⚫️ 🔢 (⚖️ 🎓) ⏮️ `**user_dict`, 🐍 🔜 "🎁" ⚫️. ⚫️ 🔜 🚶‍♀️ 🔑 &amp; 💲 `user_dict` 🔗 🔑-💲 ❌.
, ▶️ ⏮️ `user_dict` ⚪️➡️ 🔛, ✍:
```Python
UserInDB(**user_dict)
```
🔜 🏁 🕳 🌓:
```Python
UserInDB(
username="john",
password="secret",
email="john.doe@example.com",
full_name=None,
)
```
⚖️ 🌅 ⚫️❔, ⚙️ `user_dict` 🔗, ⏮️ ⚫️❔ 🎚 ⚫️ 💪 ✔️ 🔮:
```Python
UserInDB(
username = user_dict["username"],
password = user_dict["password"],
email = user_dict["email"],
full_name = user_dict["full_name"],
)
```
#### Pydantic 🏷 ⚪️➡️ 🎚 1
🖼 🔛 👥 🤚 `user_dict` ⚪️➡️ `user_in.dict()`, 👉 📟:
```Python
user_dict = user_in.dict()
UserInDB(**user_dict)
```
🔜 🌓:
```Python
UserInDB(**user_in.dict())
```
...↩️ `user_in.dict()` `dict`, &amp; ⤴️ 👥 ⚒ 🐍 "🎁" ⚫️ 🚶‍♀️ ⚫️ `UserInDB` 🔠 ⏮️ `**`.
, 👥 🤚 Pydantic 🏷 ⚪️➡️ 💽 1⃣ Pydantic 🏷.
#### 🎁 `dict` &amp; 🇨🇻
&amp; ⤴️ ❎ 🇨🇻 ❌ `hashed_password=hashed_password`, 💖:
```Python
UserInDB(**user_in.dict(), hashed_password=hashed_password)
```
...🔚 🆙 💆‍♂ 💖:
```Python
UserInDB(
username = user_dict["username"],
password = user_dict["password"],
email = user_dict["email"],
full_name = user_dict["full_name"],
hashed_password = hashed_password,
)
```
!!! warning
🔗 🌖 🔢 🤖 💪 💧 💽, ✋️ 👫 ↗️ 🚫 🚚 🙆 🎰 💂‍♂.
## 📉 ❎
📉 📟 ❎ 1⃣ 🐚 💭 **FastAPI**.
📟 ❎ 📈 🤞 🐛, 💂‍♂ ❔, 📟 🔁 ❔ (🕐❔ 👆 1⃣ 🥉 ✋️ 🚫 🎏), ♒️.
&amp; 👉 🏷 🌐 🤝 📚 💽 &amp; ❎ 🔢 📛 &amp; 🆎.
👥 💪 👻.
👥 💪 📣 `UserBase` 🏷 👈 🍦 🧢 👆 🎏 🏷. &amp; ⤴️ 👥 💪 ⚒ 🏿 👈 🏷 👈 😖 🚮 🔢 (🆎 📄, 🔬, ♒️).
🌐 💽 🛠️, 🔬, 🧾, ♒️. 🔜 👷 🛎.
👈 🌌, 👥 💪 📣 🔺 🖖 🏷 (⏮️ 🔢 `password`, ⏮️ `hashed_password` &amp; 🍵 🔐):
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="9 15-16 19-20 23-24"
{!> ../../../docs_src/extra_models/tutorial002.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="7 13-14 17-18 21-22"
{!> ../../../docs_src/extra_models/tutorial002_py310.py!}
```
## `Union` ⚖️ `anyOf`
👆 💪 📣 📨 `Union` 2⃣ 🆎, 👈 ⛓, 👈 📨 🔜 🙆 2⃣.
⚫️ 🔜 🔬 🗄 ⏮️ `anyOf`.
👈, ⚙️ 🐩 🐍 🆎 🔑 <a href="https://docs.python.org/3/library/typing.html#typing.Union" class="external-link" target="_blank">`typing.Union`</a>:
!!! note
🕐❔ ⚖ <a href="https://pydantic-docs.helpmanual.io/usage/types/#unions" class="external-link" target="_blank">`Union`</a>, 🔌 🏆 🎯 🆎 🥇, ⏩ 🌘 🎯 🆎. 🖼 🔛, 🌖 🎯 `PlaneItem` 👟 ⏭ `CarItem` `Union[PlaneItem, CarItem]`.
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="1 14-15 18-20 33"
{!> ../../../docs_src/extra_models/tutorial003.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="1 14-15 18-20 33"
{!> ../../../docs_src/extra_models/tutorial003_py310.py!}
```
### `Union` 🐍 3⃣.1⃣0
👉 🖼 👥 🚶‍♀️ `Union[PlaneItem, CarItem]` 💲 ❌ `response_model`.
↩️ 👥 🚶‍♀️ ⚫️ **💲 ❌** ↩️ 🚮 ⚫️ **🆎 ✍**, 👥 ✔️ ⚙️ `Union` 🐍 3⃣.1⃣0⃣.
🚥 ⚫️ 🆎 ✍ 👥 💪 ✔️ ⚙️ ⏸ ⏸,:
```Python
some_variable: PlaneItem | CarItem
```
✋️ 🚥 👥 🚮 👈 `response_model=PlaneItem | CarItem` 👥 🔜 🤚 ❌, ↩️ 🐍 🔜 🔄 🎭 **❌ 🛠️** 🖖 `PlaneItem` &amp; `CarItem` ↩️ 🔬 👈 🆎 ✍.
## 📇 🏷
🎏 🌌, 👆 💪 📣 📨 📇 🎚.
👈, ⚙️ 🐩 🐍 `typing.List` (⚖️ `list` 🐍 3⃣.9️⃣ &amp; 🔛):
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="1 20"
{!> ../../../docs_src/extra_models/tutorial004.py!}
```
=== "🐍 3⃣.9️⃣ &amp; 🔛"
```Python hl_lines="18"
{!> ../../../docs_src/extra_models/tutorial004_py39.py!}
```
## 📨 ⏮️ ❌ `dict`
👆 💪 📣 📨 ⚙️ ✅ ❌ `dict`, 📣 🆎 🔑 &amp; 💲, 🍵 ⚙️ Pydantic 🏷.
👉 ⚠ 🚥 👆 🚫 💭 ☑ 🏑/🔢 📛 (👈 🔜 💪 Pydantic 🏷) ⏪.
👉 💼, 👆 💪 ⚙️ `typing.Dict` (⚖️ `dict` 🐍 3⃣.9️⃣ &amp; 🔛):
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="1 8"
{!> ../../../docs_src/extra_models/tutorial005.py!}
```
=== "🐍 3⃣.9️⃣ &amp; 🔛"
```Python hl_lines="6"
{!> ../../../docs_src/extra_models/tutorial005_py39.py!}
```
## 🌃
⚙️ 💗 Pydantic 🏷 &amp; 😖 ➡ 🔠 💼.
👆 🚫 💪 ✔️ 👁 💽 🏷 📍 👨‍💼 🚥 👈 👨‍💼 🔜 💪 ✔️ 🎏 "🇵🇸". 💼 ⏮️ 👩‍💻 "👨‍💼" ⏮️ 🇵🇸 ✅ `password`, `password_hash` &amp; 🙅‍♂ 🔐.

View File

@@ -0,0 +1,333 @@
# 🥇 🔁
🙅 FastAPI 📁 💪 👀 💖 👉:
```Python
{!../../../docs_src/first_steps/tutorial001.py!}
```
📁 👈 📁 `main.py`.
🏃 🖖 💽:
<div class="termy">
```console
$ uvicorn main:app --reload
<span style="color: green;">INFO</span>: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
<span style="color: green;">INFO</span>: Started reloader process [28720]
<span style="color: green;">INFO</span>: Started server process [28722]
<span style="color: green;">INFO</span>: Waiting for application startup.
<span style="color: green;">INFO</span>: Application startup complete.
```
</div>
!!! note
📋 `uvicorn main:app` 🔗:
* `main`: 📁 `main.py` (🐍 "🕹").
* `app`: 🎚 ✍ 🔘 `main.py` ⏮️ ⏸ `app = FastAPI()`.
* `--reload`: ⚒ 💽 ⏏ ⏮️ 📟 🔀. 🕴 ⚙️ 🛠️.
🔢, 📤 ⏸ ⏮️ 🕳 💖:
```hl_lines="4"
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
```
👈 ⏸ 🎦 📛 🌐❔ 👆 📱 🍦, 👆 🇧🇿 🎰.
### ✅ ⚫️
📂 👆 🖥 <a href="http://127.0.0.1:8000" class="external-link" target="_blank">http://127.0.0.1:8000</a>.
👆 🔜 👀 🎻 📨:
```JSON
{"message": "Hello World"}
```
### 🎓 🛠️ 🩺
🔜 🚶 <a href="http://127.0.0.1:8000/docs" class="external-link" target="_blank">http://127.0.0.1:8000/docs</a>.
👆 🔜 👀 🏧 🎓 🛠️ 🧾 (🚚 <a href="https://github.com/swagger-api/swagger-ui" class="external-link" target="_blank">🦁 🎚</a>):
![Swagger UI](https://fastapi.tiangolo.com/img/index/index-01-swagger-ui-simple.png)
### 🎛 🛠️ 🩺
&amp; 🔜, 🚶 <a href="http://127.0.0.1:8000/redoc" class="external-link" target="_blank">http://127.0.0.1:8000/redoc</a>.
👆 🔜 👀 🎛 🏧 🧾 (🚚 <a href="https://github.com/Rebilly/ReDoc" class="external-link" target="_blank">📄</a>):
![ReDoc](https://fastapi.tiangolo.com/img/index/index-02-redoc-simple.png)
### 🗄
**FastAPI** 🏗 "🔗" ⏮️ 🌐 👆 🛠️ ⚙️ **🗄** 🐩 ⚖ 🔗.
#### "🔗"
"🔗" 🔑 ⚖️ 📛 🕳. 🚫 📟 👈 🛠️ ⚫️, ✋️ 📝 📛.
#### 🛠️ "🔗"
👉 💼, <a href="https://github.com/OAI/OpenAPI-Specification" class="external-link" target="_blank">🗄</a> 🔧 👈 🤔 ❔ 🔬 🔗 👆 🛠️.
👉 🔗 🔑 🔌 👆 🛠️ ➡, 💪 🔢 👫 ✊, ♒️.
#### 💽 "🔗"
⚖ "🔗" 💪 🔗 💠 💽, 💖 🎻 🎚.
👈 💼, ⚫️ 🔜 ⛓ 🎻 🔢, &amp; 📊 🆎 👫 ✔️, ♒️.
#### 🗄 &amp; 🎻 🔗
🗄 🔬 🛠️ 🔗 👆 🛠️. &amp; 👈 🔗 🔌 🔑 (⚖️ "🔗") 📊 📨 &amp; 📨 👆 🛠️ ⚙️ **🎻 🔗**, 🐩 🎻 📊 🔗.
#### ✅ `openapi.json`
🚥 👆 😟 🔃 ❔ 🍣 🗄 🔗 👀 💖, FastAPI 🔁 🏗 🎻 (🔗) ⏮️ 📛 🌐 👆 🛠️.
👆 💪 👀 ⚫️ 🔗: <a href="http://127.0.0.1:8000/openapi.json" class="external-link" target="_blank">http://127.0.0.1:8000/openapi.json</a>.
⚫️ 🔜 🎦 🎻 ▶️ ⏮️ 🕳 💖:
```JSON
{
"openapi": "3.0.2",
"info": {
"title": "FastAPI",
"version": "0.1.0"
},
"paths": {
"/items/": {
"get": {
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
...
```
#### ⚫️❔ 🗄
🗄 🔗 ⚫️❔ 🏋️ 2⃣ 🎓 🧾 ⚙️ 🔌.
&amp; 📤 💯 🎛, 🌐 ⚓️ 🔛 🗄. 👆 💪 💪 🚮 🙆 📚 🎛 👆 🈸 🏗 ⏮️ **FastAPI**.
👆 💪 ⚙️ ⚫️ 🏗 📟 🔁, 👩‍💻 👈 🔗 ⏮️ 👆 🛠️. 🖼, 🕸, 📱 ⚖️ ☁ 🈸.
## 🌃, 🔁 🔁
### 🔁 1⃣: 🗄 `FastAPI`
```Python hl_lines="1"
{!../../../docs_src/first_steps/tutorial001.py!}
```
`FastAPI` 🐍 🎓 👈 🚚 🌐 🛠️ 👆 🛠️.
!!! note "📡 "
`FastAPI` 🎓 👈 😖 🔗 ⚪️➡️ `Starlette`.
👆 💪 ⚙️ 🌐 <a href="https://www.starlette.io/" class="external-link" target="_blank">💃</a> 🛠️ ⏮️ `FastAPI` 💁‍♂️.
### 🔁 2⃣: ✍ `FastAPI` "👐"
```Python hl_lines="3"
{!../../../docs_src/first_steps/tutorial001.py!}
```
📥 `app` 🔢 🔜 "👐" 🎓 `FastAPI`.
👉 🔜 👑 ☝ 🔗 ✍ 🌐 👆 🛠️.
👉 `app` 🎏 1⃣ 🔗 `uvicorn` 📋:
<div class="termy">
```console
$ uvicorn main:app --reload
<span style="color: green;">INFO</span>: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
```
</div>
🚥 👆 ✍ 👆 📱 💖:
```Python hl_lines="3"
{!../../../docs_src/first_steps/tutorial002.py!}
```
&amp; 🚮 ⚫️ 📁 `main.py`, ⤴️ 👆 🔜 🤙 `uvicorn` 💖:
<div class="termy">
```console
$ uvicorn main:my_awesome_api --reload
<span style="color: green;">INFO</span>: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
```
</div>
### 🔁 3⃣: ✍ *➡ 🛠️*
#### ➡
"➡" 📥 🔗 🏁 🍕 📛 ▶️ ⚪️➡️ 🥇 `/`.
, 📛 💖:
```
https://example.com/items/foo
```
...➡ 🔜:
```
/items/foo
```
!!! info
"➡" 🛎 🤙 "🔗" ⚖️ "🛣".
⏪ 🏗 🛠️, "➡" 👑 🌌 🎏 "⚠" &amp; "".
#### 🛠️
"🛠️" 📥 🔗 1⃣ 🇺🇸🔍 "👩‍🔬".
1⃣:
* `POST`
* `GET`
* `PUT`
* `DELETE`
...&amp; 🌅 😍 🕐:
* `OPTIONS`
* `HEAD`
* `PATCH`
* `TRACE`
🇺🇸🔍 🛠️, 👆 💪 🔗 🔠 ➡ ⚙️ 1⃣ (⚖️ 🌅) 👫 "👩‍🔬".
---
🕐❔ 🏗 🔗, 👆 🛎 ⚙️ 👫 🎯 🇺🇸🔍 👩‍🔬 🎭 🎯 🎯.
🛎 👆 ⚙️:
* `POST`: ✍ 💽.
* `GET`: ✍ 💽.
* `PUT`: 💽.
* `DELETE`: ❎ 💽.
, 🗄, 🔠 🇺🇸🔍 👩‍🔬 🤙 "🛠️".
👥 🔜 🤙 👫 "**🛠️**" 💁‍♂️.
#### 🔬 *➡ 🛠️ 👨‍🎨*
```Python hl_lines="6"
{!../../../docs_src/first_steps/tutorial001.py!}
```
`@app.get("/")` 💬 **FastAPI** 👈 🔢 ▶️️ 🔛 🈚 🚚 📨 👈 🚶:
* ➡ `/`
* ⚙️ <abbr title="an HTTP GET method"><code>get</code> 🛠️</abbr>
!!! info "`@decorator` "
👈 `@something` ❕ 🐍 🤙 "👨‍🎨".
👆 🚮 ⚫️ 🔛 🔝 🔢. 💖 📶 📔 👒 (👤 💭 👈 🌐❔ ⚖ 👟 ⚪️➡️).
"👨‍🎨" ✊ 🔢 🔛 &amp; 🔨 🕳 ⏮️ ⚫️.
👆 💼, 👉 👨‍🎨 💬 **FastAPI** 👈 🔢 🔛 🔗 **➡** `/` ⏮️ **🛠️** `get`.
⚫️ "**➡ 🛠️ 👨‍🎨**".
👆 💪 ⚙️ 🎏 🛠️:
* `@app.post()`
* `@app.put()`
* `@app.delete()`
&amp; 🌅 😍 🕐:
* `@app.options()`
* `@app.head()`
* `@app.patch()`
* `@app.trace()`
!!! tip
👆 🆓 ⚙️ 🔠 🛠️ (🇺🇸🔍 👩‍🔬) 👆 🎋.
**FastAPI** 🚫 🛠️ 🙆 🎯 🔑.
📥 🎁 📄, 🚫 📄.
🖼, 🕐❔ ⚙️ 🕹 👆 🛎 🎭 🌐 🎯 ⚙️ 🕴 `POST` 🛠️.
### 🔁 4⃣: 🔬 **➡ 🛠️ 🔢**
👉 👆 "**➡ 🛠️ 🔢**":
* **➡**: `/`.
* **🛠️**: `get`.
* **🔢**: 🔢 🔛 "👨‍🎨" (🔛 `@app.get("/")`).
```Python hl_lines="7"
{!../../../docs_src/first_steps/tutorial001.py!}
```
👉 🐍 🔢.
⚫️ 🔜 🤙 **FastAPI** 🕐❔ ⚫️ 📨 📨 📛 "`/`" ⚙️ `GET` 🛠️.
👉 💼, ⚫️ `async` 🔢.
---
👆 💪 🔬 ⚫️ 😐 🔢 ↩️ `async def`:
```Python hl_lines="7"
{!../../../docs_src/first_steps/tutorial003.py!}
```
!!! note
🚥 👆 🚫 💭 🔺, ✅ [🔁: *"🏃 ❓"*](../async.md#in-a-hurry){.internal-link target=_blank}.
### 🔁 5⃣: 📨 🎚
```Python hl_lines="8"
{!../../../docs_src/first_steps/tutorial001.py!}
```
👆 💪 📨 `dict`, `list`, ⭐ 💲 `str`, `int`, ♒️.
👆 💪 📨 Pydantic 🏷 (👆 🔜 👀 🌅 🔃 👈 ⏪).
📤 📚 🎏 🎚 &amp; 🏷 👈 🔜 🔁 🗜 🎻 (🔌 🐜, ♒️). 🔄 ⚙️ 👆 💕 🕐, ⚫️ 🏆 🎲 👈 👫 ⏪ 🐕‍🦺.
## 🌃
* 🗄 `FastAPI`.
* ✍ `app` 👐.
* ✍ **➡ 🛠️ 👨‍🎨** (💖 `@app.get("/")`).
* ✍ **➡ 🛠️ 🔢** (💖 `def root(): ...` 🔛).
* 🏃 🛠️ 💽 (💖 `uvicorn main:app --reload`).

View File

@@ -0,0 +1,261 @@
# 🚚 ❌
📤 📚 ⚠ 🌐❔ 👆 💪 🚨 ❌ 👩‍💻 👈 ⚙️ 👆 🛠️.
👉 👩‍💻 💪 🖥 ⏮️ 🕸, 📟 ⚪️➡️ 👱 🙆, ☁ 📳, ♒️.
👆 💪 💪 💬 👩‍💻 👈:
* 👩‍💻 🚫 ✔️ 🥃 😌 👈 🛠️.
* 👩‍💻 🚫 ✔️ 🔐 👈 .
* 🏬 👩‍💻 🔄 🔐 🚫 🔀.
* ♒️.
👫 💼, 👆 🔜 🛎 📨 **🇺🇸🔍 👔 📟** ↔ **4⃣0⃣0** (⚪️➡️ 4⃣0⃣0⃣ 4⃣9⃣9⃣).
👉 🎏 2⃣0⃣0⃣ 🇺🇸🔍 👔 📟 (⚪️➡️ 2⃣0⃣0⃣ 2⃣9⃣9⃣). 👈 "2⃣0⃣0⃣" 👔 📟 ⛓ 👈 😫 📤 "🏆" 📨.
👔 📟 4⃣0⃣0⃣ ↔ ⛓ 👈 📤 ❌ ⚪️➡️ 👩‍💻.
💭 🌐 👈 **"4⃣0⃣4⃣ 🚫 🔎"** ❌ (&amp; 🤣) ❓
## ⚙️ `HTTPException`
📨 🇺🇸🔍 📨 ⏮️ ❌ 👩‍💻 👆 ⚙️ `HTTPException`.
### 🗄 `HTTPException`
```Python hl_lines="1"
{!../../../docs_src/handling_errors/tutorial001.py!}
```
### 🤚 `HTTPException` 👆 📟
`HTTPException` 😐 🐍 ⚠ ⏮️ 🌖 📊 🔗 🔗.
↩️ ⚫️ 🐍 ⚠, 👆 🚫 `return` ⚫️, 👆 `raise` ⚫️.
👉 ⛓ 👈 🚥 👆 🔘 🚙 🔢 👈 👆 🤙 🔘 👆 *➡ 🛠️ 🔢*, &amp; 👆 🤚 `HTTPException` ⚪️➡️ 🔘 👈 🚙 🔢, ⚫️ 🏆 🚫 🏃 🎂 📟 *➡ 🛠️ 🔢*, ⚫️ 🔜 ❎ 👈 📨 ▶️️ ↖️ &amp; 📨 🇺🇸🔍 ❌ ⚪️➡️ `HTTPException` 👩‍💻.
💰 🙋‍♀ ⚠ 🤭 `return`😅 💲 🔜 🌖 ⭐ 📄 🔃 🔗 &amp; 💂‍♂.
👉 🖼, 🕐❔ 👩‍💻 📨 🏬 🆔 👈 🚫 🔀, 🤚 ⚠ ⏮️ 👔 📟 `404`:
```Python hl_lines="11"
{!../../../docs_src/handling_errors/tutorial001.py!}
```
### 📉 📨
🚥 👩‍💻 📨 `http://example.com/items/foo` ( `item_id` `"foo"`), 👈 👩‍💻 🔜 📨 🇺🇸🔍 👔 📟 2⃣0⃣0⃣, &amp; 🎻 📨:
```JSON
{
"item": "The Foo Wrestlers"
}
```
✋️ 🚥 👩‍💻 📨 `http://example.com/items/bar` (🚫-🚫 `item_id` `"bar"`), 👈 👩‍💻 🔜 📨 🇺🇸🔍 👔 📟 4⃣0⃣4⃣ ("🚫 🔎" ❌), &amp; 🎻 📨:
```JSON
{
"detail": "Item not found"
}
```
!!! tip
🕐❔ 🙋‍♀ `HTTPException`, 👆 💪 🚶‍♀️ 🙆 💲 👈 💪 🗜 🎻 🔢 `detail`, 🚫 🕴 `str`.
👆 💪 🚶‍♀️ `dict`, `list`, ♒️.
👫 🍵 🔁 **FastAPI** &amp; 🗜 🎻.
## 🚮 🛃 🎚
📤 ⚠ 🌐❔ ⚫️ ⚠ 💪 🚮 🛃 🎚 🇺🇸🔍 ❌. 🖼, 🆎 💂‍♂.
👆 🎲 🏆 🚫 💪 ⚙️ ⚫️ 🔗 👆 📟.
✋️ 💼 👆 💪 ⚫️ 🏧 😐, 👆 💪 🚮 🛃 🎚:
```Python hl_lines="14"
{!../../../docs_src/handling_errors/tutorial002.py!}
```
## ❎ 🛃 ⚠ 🐕‍🦺
👆 💪 🚮 🛃 ⚠ 🐕‍🦺 ⏮️ <a href="https://www.starlette.io/exceptions/" class="external-link" target="_blank">🎏 ⚠ 🚙 ⚪️➡️ 💃</a>.
➡️ 💬 👆 ✔️ 🛃 ⚠ `UnicornException` 👈 👆 (⚖️ 🗃 👆 ⚙️) 💪 `raise`.
&amp; 👆 💚 🍵 👉 ⚠ 🌐 ⏮️ FastAPI.
👆 💪 🚮 🛃 ⚠ 🐕‍🦺 ⏮️ `@app.exception_handler()`:
```Python hl_lines="5-7 13-18 24"
{!../../../docs_src/handling_errors/tutorial003.py!}
```
📥, 🚥 👆 📨 `/unicorns/yolo`, *➡ 🛠️* 🔜 `raise` `UnicornException`.
✋️ ⚫️ 🔜 🍵 `unicorn_exception_handler`.
, 👆 🔜 📨 🧹 ❌, ⏮️ 🇺🇸🔍 👔 📟 `418` &amp; 🎻 🎚:
```JSON
{"message": "Oops! yolo did something. There goes a rainbow..."}
```
!!! note "📡 "
👆 💪 ⚙️ `from starlette.requests import Request` &amp; `from starlette.responses import JSONResponse`.
**FastAPI** 🚚 🎏 `starlette.responses` `fastapi.responses` 🏪 👆, 👩‍💻. ✋️ 🌅 💪 📨 👟 🔗 ⚪️➡️ 💃. 🎏 ⏮️ `Request`.
## 🔐 🔢 ⚠ 🐕‍🦺
**FastAPI** ✔️ 🔢 ⚠ 🐕‍🦺.
👫 🐕‍🦺 🈚 🛬 🔢 🎻 📨 🕐❔ 👆 `raise` `HTTPException` &amp; 🕐❔ 📨 ✔️ ❌ 💽.
👆 💪 🔐 👫 ⚠ 🐕‍🦺 ⏮️ 👆 👍.
### 🔐 📨 🔬 ⚠
🕐❔ 📨 🔌 ❌ 📊, **FastAPI** 🔘 🤚 `RequestValidationError`.
&amp; ⚫️ 🔌 🔢 ⚠ 🐕‍🦺 ⚫️.
🔐 ⚫️, 🗄 `RequestValidationError` &amp; ⚙️ ⚫️ ⏮️ `@app.exception_handler(RequestValidationError)` 🎀 ⚠ 🐕‍🦺.
⚠ 🐕‍🦺 🔜 📨 `Request` &amp; ⚠.
```Python hl_lines="2 14-16"
{!../../../docs_src/handling_errors/tutorial004.py!}
```
🔜, 🚥 👆 🚶 `/items/foo`, ↩️ 💆‍♂ 🔢 🎻 ❌ ⏮️:
```JSON
{
"detail": [
{
"loc": [
"path",
"item_id"
],
"msg": "value is not a valid integer",
"type": "type_error.integer"
}
]
}
```
👆 🔜 🤚 ✍ ⏬, ⏮️:
```
1 validation error
path -> item_id
value is not a valid integer (type=type_error.integer)
```
#### `RequestValidationError` 🆚 `ValidationError`
!!! warning
👫 📡 👈 👆 💪 🚶 🚥 ⚫️ 🚫 ⚠ 👆 🔜.
`RequestValidationError` 🎧-🎓 Pydantic <a href="https://pydantic-docs.helpmanual.io/usage/models/#error-handling" class="external-link" target="_blank">`ValidationError`</a>.
**FastAPI** ⚙️ ⚫️ 👈, 🚥 👆 ⚙️ Pydantic 🏷 `response_model`, &amp; 👆 💽 ✔️ ❌, 👆 🔜 👀 ❌ 👆 🕹.
✋️ 👩‍💻/👩‍💻 🔜 🚫 👀 ⚫️. ↩️, 👩‍💻 🔜 📨 "🔗 💽 ❌" ⏮️ 🇺🇸🔍 👔 📟 `500`.
⚫️ 🔜 👉 🌌 ↩️ 🚥 👆 ✔️ Pydantic `ValidationError` 👆 *📨* ⚖️ 🙆 👆 📟 (🚫 👩‍💻 *📨*), ⚫️ 🤙 🐛 👆 📟.
&amp; ⏪ 👆 🔧 ⚫️, 👆 👩‍💻/👩‍💻 🚫🔜 🚫 ✔️ 🔐 🔗 🔃 ❌, 👈 💪 🎦 💂‍♂ ⚠.
### 🔐 `HTTPException` ❌ 🐕‍🦺
🎏 🌌, 👆 💪 🔐 `HTTPException` 🐕‍🦺.
🖼, 👆 💪 💚 📨 ✅ ✍ 📨 ↩️ 🎻 👫 ❌:
```Python hl_lines="3-4 9-11 22"
{!../../../docs_src/handling_errors/tutorial004.py!}
```
!!! note "📡 "
👆 💪 ⚙️ `from starlette.responses import PlainTextResponse`.
**FastAPI** 🚚 🎏 `starlette.responses` `fastapi.responses` 🏪 👆, 👩‍💻. ✋️ 🌅 💪 📨 👟 🔗 ⚪️➡️ 💃.
### ⚙️ `RequestValidationError` 💪
`RequestValidationError` 🔌 `body` ⚫️ 📨 ⏮️ ❌ 💽.
👆 💪 ⚙️ ⚫️ ⏪ 🛠️ 👆 📱 🕹 💪 &amp; ⚫️, 📨 ⚫️ 👩‍💻, ♒️.
```Python hl_lines="14"
{!../../../docs_src/handling_errors/tutorial005.py!}
```
🔜 🔄 📨 ❌ 🏬 💖:
```JSON
{
"title": "towel",
"size": "XL"
}
```
👆 🔜 📨 📨 💬 👆 👈 💽 ❌ ⚗ 📨 💪:
```JSON hl_lines="12-15"
{
"detail": [
{
"loc": [
"body",
"size"
],
"msg": "value is not a valid integer",
"type": "type_error.integer"
}
],
"body": {
"title": "towel",
"size": "XL"
}
}
```
#### FastAPI `HTTPException` 🆚 💃 `HTTPException`
**FastAPI** ✔️ 🚮 👍 `HTTPException`.
&amp; **FastAPI**'Ⓜ `HTTPException` ❌ 🎓 😖 ⚪️➡️ 💃 `HTTPException` ❌ 🎓.
🕴 🔺, 👈 **FastAPI**'Ⓜ `HTTPException` ✔ 👆 🚮 🎚 🔌 📨.
👉 💪/⚙️ 🔘 ✳ 2⃣.0️⃣ &amp; 💂‍♂ 🚙.
, 👆 💪 🚧 🙋‍♀ **FastAPI**'Ⓜ `HTTPException` 🛎 👆 📟.
✋️ 🕐❔ 👆 ® ⚠ 🐕‍🦺, 👆 🔜 ® ⚫️ 💃 `HTTPException`.
👉 🌌, 🚥 🙆 🍕 💃 🔗 📟, ⚖️ 💃 ↔ ⚖️ 🔌 -, 🤚 💃 `HTTPException`, 👆 🐕‍🦺 🔜 💪 ✊ &amp; 🍵 ⚫️.
👉 🖼, 💪 ✔️ 👯‍♂️ `HTTPException`Ⓜ 🎏 📟, 💃 ⚠ 📁 `StarletteHTTPException`:
```Python
from starlette.exceptions import HTTPException as StarletteHTTPException
```
### 🏤-⚙️ **FastAPI**'Ⓜ ⚠ 🐕‍🦺
🚥 👆 💚 ⚙️ ⚠ ⤴️ ⏮️ 🎏 🔢 ⚠ 🐕‍🦺 ⚪️➡️ **FastAPI**, 👆 💪 🗄 &amp; 🏤-⚙️ 🔢 ⚠ 🐕‍🦺 ⚪️➡️ `fastapi.exception_handlers`:
```Python hl_lines="2-5 15 21"
{!../../../docs_src/handling_errors/tutorial006.py!}
```
👉 🖼 👆 `print`😅 ❌ ⏮️ 📶 🎨 📧, ✋️ 👆 🤚 💭. 👆 💪 ⚙️ ⚠ &amp; ⤴️ 🏤-⚙️ 🔢 ⚠ 🐕‍🦺.

View File

@@ -0,0 +1,128 @@
# 🎚 🔢
👆 💪 🔬 🎚 🔢 🎏 🌌 👆 🔬 `Query`, `Path` &amp; `Cookie` 🔢.
## 🗄 `Header`
🥇 🗄 `Header`:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="3"
{!> ../../../docs_src/header_params/tutorial001.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="1"
{!> ../../../docs_src/header_params/tutorial001_py310.py!}
```
## 📣 `Header` 🔢
⤴️ 📣 🎚 🔢 ⚙️ 🎏 📊 ⏮️ `Path`, `Query` &amp; `Cookie`.
🥇 💲 🔢 💲, 👆 💪 🚶‍♀️ 🌐 🔬 ⚖️ ✍ 🔢:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="9"
{!> ../../../docs_src/header_params/tutorial001.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="7"
{!> ../../../docs_src/header_params/tutorial001_py310.py!}
```
!!! note "📡 "
`Header` "👭" 🎓 `Path`, `Query` &amp; `Cookie`. ⚫️ 😖 ⚪️➡️ 🎏 ⚠ `Param` 🎓.
✋️ 💭 👈 🕐❔ 👆 🗄 `Query`, `Path`, `Header`, &amp; 🎏 ⚪️➡️ `fastapi`, 👈 🤙 🔢 👈 📨 🎁 🎓.
!!! info
📣 🎚, 👆 💪 ⚙️ `Header`, ↩️ ⏪ 🔢 🔜 🔬 🔢 🔢.
## 🏧 🛠️
`Header` ✔️ 🐥 🛠️ 🔛 🔝 ⚫️❔ `Path`, `Query` &amp; `Cookie` 🚚.
🌅 🐩 🎚 🎏 "🔠" 🦹, 💭 " 🔣" (`-`).
✋️ 🔢 💖 `user-agent` ❌ 🐍.
, 🔢, `Header` 🔜 🗜 🔢 📛 🦹 ⚪️➡️ 🎦 (`_`) 🔠 (`-`) ⚗ &amp; 📄 🎚.
, 🇺🇸🔍 🎚 💼-😛,, 👆 💪 📣 👫 ⏮️ 🐩 🐍 👗 (💭 "🔡").
, 👆 💪 ⚙️ `user_agent` 👆 🛎 🔜 🐍 📟, ↩️ 💆‍♂ 🎯 🥇 🔤 `User_Agent` ⚖️ 🕳 🎏.
🚥 🤔 👆 💪 ❎ 🏧 🛠️ 🎦 🔠, ⚒ 🔢 `convert_underscores` `Header` `False`:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="10"
{!> ../../../docs_src/header_params/tutorial002.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="8"
{!> ../../../docs_src/header_params/tutorial002_py310.py!}
```
!!! warning
⏭ ⚒ `convert_underscores` `False`, 🐻 🤯 👈 🇺🇸🔍 🗳 &amp; 💽 / ⚙️ 🎚 ⏮️ 🎦.
## ❎ 🎚
⚫️ 💪 📨 ❎ 🎚. 👈 ⛓, 🎏 🎚 ⏮️ 💗 💲.
👆 💪 🔬 👈 💼 ⚙️ 📇 🆎 📄.
👆 🔜 📨 🌐 💲 ⚪️➡️ ❎ 🎚 🐍 `list`.
🖼, 📣 🎚 `X-Token` 👈 💪 😑 🌅 🌘 🕐, 👆 💪 ✍:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="9"
{!> ../../../docs_src/header_params/tutorial003.py!}
```
=== "🐍 3⃣.9️⃣ &amp; 🔛"
```Python hl_lines="9"
{!> ../../../docs_src/header_params/tutorial003_py39.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="7"
{!> ../../../docs_src/header_params/tutorial003_py310.py!}
```
🚥 👆 🔗 ⏮️ 👈 *➡ 🛠️* 📨 2⃣ 🇺🇸🔍 🎚 💖:
```
X-Token: foo
X-Token: bar
```
📨 🔜 💖:
```JSON
{
"X-Token values": [
"bar",
"foo"
]
}
```
## 🌃
📣 🎚 ⏮️ `Header`, ⚙️ 🎏 ⚠ ⚓ `Query`, `Path` &amp; `Cookie`.
&amp; 🚫 😟 🔃 🎦 👆 🔢, **FastAPI** 🔜 ✊ 💅 🏭 👫.

View File

@@ -0,0 +1,80 @@
# 🔰 - 👩‍💻 🦮 - 🎶
👉 🔰 🎦 👆 ❔ ⚙️ **FastAPI** ⏮️ 🌅 🚮 ⚒, 🔁 🔁.
🔠 📄 📉 🏗 🔛 ⏮️ 🕐, ✋️ ⚫️ 🏗 🎏 ❔, 👈 👆 💪 🚶 🔗 🙆 🎯 1⃣ ❎ 👆 🎯 🛠️ 💪.
⚫️ 🏗 👷 🔮 🔗.
👆 💪 👟 🔙 &amp; 👀 ⚫️❔ ⚫️❔ 👆 💪.
## 🏃 📟
🌐 📟 🍫 💪 📁 &amp; ⚙️ 🔗 (👫 🤙 💯 🐍 📁).
🏃 🙆 🖼, 📁 📟 📁 `main.py`, &amp; ▶️ `uvicorn` ⏮️:
<div class="termy">
```console
$ uvicorn main:app --reload
<span style="color: green;">INFO</span>: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
<span style="color: green;">INFO</span>: Started reloader process [28720]
<span style="color: green;">INFO</span>: Started server process [28722]
<span style="color: green;">INFO</span>: Waiting for application startup.
<span style="color: green;">INFO</span>: Application startup complete.
```
</div>
⚫️ **🏆 💡** 👈 👆 ✍ ⚖️ 📁 📟, ✍ ⚫️ &amp; 🏃 ⚫️ 🌐.
⚙️ ⚫️ 👆 👨‍🎨 ⚫️❔ 🤙 🎦 👆 💰 FastAPI, 👀 ❔ 🐥 📟 👆 ✔️ ✍, 🌐 🆎 ✅, ✍, ♒️.
---
## ❎ FastAPI
🥇 🔁 ❎ FastAPI.
🔰, 👆 💪 💚 ❎ ⚫️ ⏮️ 🌐 📦 🔗 &amp; ⚒:
<div class="termy">
```console
$ pip install "fastapi[all]"
---> 100%
```
</div>
...👈 🔌 `uvicorn`, 👈 👆 💪 ⚙️ 💽 👈 🏃 👆 📟.
!!! note
👆 💪 ❎ ⚫️ 🍕 🍕.
👉 ⚫️❔ 👆 🔜 🎲 🕐 👆 💚 🛠️ 👆 🈸 🏭:
```
pip install fastapi
```
❎ `uvicorn` 👷 💽:
```
pip install "uvicorn[standard]"
```
&amp; 🎏 🔠 📦 🔗 👈 👆 💚 ⚙️.
## 🏧 👩‍💻 🦮
📤 **🏧 👩‍💻 🦮** 👈 👆 💪 ✍ ⏪ ⏮️ 👉 **🔰 - 👩‍💻 🦮**.
**🏧 👩‍💻 🦮**, 🏗 🔛 👉, ⚙️ 🎏 🔧, &amp; 💡 👆 ⚒.
✋️ 👆 🔜 🥇 ✍ **🔰 - 👩‍💻 🦮** (⚫️❔ 👆 👂 ▶️️ 🔜).
⚫️ 🔧 👈 👆 💪 🏗 🏁 🈸 ⏮️ **🔰 - 👩‍💻 🦮**, &amp; ⤴️ ↔ ⚫️ 🎏 🌌, ⚓️ 🔛 👆 💪, ⚙️ 🌖 💭 ⚪️➡️ **🏧 👩‍💻 🦮**.

View File

@@ -0,0 +1,112 @@
# 🗃 &amp; 🩺 📛
👆 💪 🛃 📚 🗃 📳 👆 **FastAPI** 🈸.
## 🗃 🛠️
👆 💪 ⚒ 📄 🏑 👈 ⚙️ 🗄 🔧 &amp; 🏧 🛠️ 🩺 ⚜:
| 🔢 | 🆎 | 📛 |
|------------|------|-------------|
| `title` | `str` | 📛 🛠️. |
| `description` | `str` | 📏 📛 🛠️. ⚫️ 💪 ⚙️ ✍. |
| `version` | `string` | ⏬ 🛠️. 👉 ⏬ 👆 👍 🈸, 🚫 🗄. 🖼 `2.5.0`. |
| `terms_of_service` | `str` | 📛 ⚖ 🐕‍🦺 🛠️. 🚥 🚚, 👉 ✔️ 📛. |
| `contact` | `dict` | 📧 🎦 🛠️. ⚫️ 💪 🔌 📚 🏑. <details><summary><code>contact</code> 🏑</summary><table><thead><tr><th>🔢</th><th>🆎</th><th>📛</th></tr></thead><tbody><tr><td><code>name</code></td><td><code>str</code></td><td>⚖ 📛 📧 👨‍💼/🏢.</td></tr><tr><td><code>url</code></td><td><code>str</code></td><td>📛 ☝ 📧 . 🔜 📁 📛.</td></tr><tr><td><code>email</code></td><td><code>str</code></td><td>📧 📢 📧 👨‍💼/🏢. 🔜 📁 📧 📢. </td></tr></tbody></table></details> |
| `license_info` | `dict` | 🛂 🎦 🛠️. ⚫️ 💪 🔌 📚 🏑. <details><summary><code>license_info</code> 🏑</summary><table><thead><tr><th>🔢</th><th>🆎</th><th>📛</th></tr></thead><tbody><tr><td><code>name</code></td><td><code>str</code></td><td><strong>🚚</strong> (🚥 <code>license_info</code> ⚒). 🛂 📛 ⚙️ 🛠️.</td></tr><tr><td><code>url</code></td><td><code>str</code></td><td>📛 🛂 ⚙️ 🛠️. 🔜 📁 📛. </td></tr></tbody></table></details> |
👆 💪 ⚒ 👫 ⏩:
```Python hl_lines="3-16 19-31"
{!../../../docs_src/metadata/tutorial001.py!}
```
!!! tip
👆 💪 ✍ ✍ `description` 🏑 &amp; ⚫️ 🔜 ✍ 🔢.
⏮️ 👉 📳, 🏧 🛠️ 🩺 🔜 👀 💖:
<img src="/img/tutorial/metadata/image01.png">
## 🗃 🔖
👆 💪 🚮 🌖 🗃 🎏 🔖 ⚙️ 👪 👆 ➡ 🛠️ ⏮️ 🔢 `openapi_tags`.
⚫️ ✊ 📇 ⚗ 1⃣ 📖 🔠 🔖.
🔠 📖 💪 🔌:
* `name` (**✔**): `str` ⏮️ 🎏 📛 👆 ⚙️ `tags` 🔢 👆 *➡ 🛠️* &amp; `APIRouter`Ⓜ.
* `description`: `str` ⏮️ 📏 📛 🔖. ⚫️ 💪 ✔️ ✍ &amp; 🔜 🎦 🩺 🎚.
* `externalDocs`: `dict` 🔬 🔢 🧾 ⏮️:
* `description`: `str` ⏮️ 📏 📛 🔢 🩺.
* `url` (**✔**): `str` ⏮️ 📛 🔢 🧾.
### ✍ 🗃 🔖
➡️ 🔄 👈 🖼 ⏮️ 🔖 `users` &amp; `items`.
✍ 🗃 👆 🔖 &amp; 🚶‍♀️ ⚫️ `openapi_tags` 🔢:
```Python hl_lines="3-16 18"
{!../../../docs_src/metadata/tutorial004.py!}
```
👀 👈 👆 💪 ⚙️ ✍ 🔘 📛, 🖼 "💳" 🔜 🎦 🦁 (**💳**) &amp; "🎀" 🔜 🎦 ❕ (_🎀_).
!!! tip
👆 🚫 ✔️ 🚮 🗃 🌐 🔖 👈 👆 ⚙️.
### ⚙️ 👆 🔖
⚙️ `tags` 🔢 ⏮️ 👆 *➡ 🛠️* (&amp; `APIRouter`Ⓜ) 🛠️ 👫 🎏 🔖:
```Python hl_lines="21 26"
{!../../../docs_src/metadata/tutorial004.py!}
```
!!! info
✍ 🌅 🔃 🔖 [➡ 🛠️ 📳](../path-operation-configuration/#tags){.internal-link target=_blank}.
### ✅ 🩺
🔜, 🚥 👆 ✅ 🩺, 👫 🔜 🎦 🌐 🌖 🗃:
<img src="/img/tutorial/metadata/image02.png">
### ✔ 🔖
✔ 🔠 🔖 🗃 📖 🔬 ✔ 🎦 🩺 🎚.
🖼, ✋️ `users` 🔜 🚶 ⏮️ `items` 🔤 ✔, ⚫️ 🎦 ⏭ 👫, ↩️ 👥 🚮 👫 🗃 🥇 📖 📇.
## 🗄 📛
🔢, 🗄 🔗 🍦 `/openapi.json`.
✋️ 👆 💪 🔗 ⚫️ ⏮️ 🔢 `openapi_url`.
🖼, ⚒ ⚫️ 🍦 `/api/v1/openapi.json`:
```Python hl_lines="3"
{!../../../docs_src/metadata/tutorial002.py!}
```
🚥 👆 💚 ❎ 🗄 🔗 🍕 👆 💪 ⚒ `openapi_url=None`, 👈 🔜 ❎ 🧾 👩‍💻 🔢 👈 ⚙️ ⚫️.
## 🩺 📛
👆 💪 🔗 2⃣ 🧾 👩‍💻 🔢 🔌:
* **🦁 🎚**: 🍦 `/docs`.
* 👆 💪 ⚒ 🚮 📛 ⏮️ 🔢 `docs_url`.
* 👆 💪 ❎ ⚫️ ⚒ `docs_url=None`.
* **📄**: 🍦 `/redoc`.
* 👆 💪 ⚒ 🚮 📛 ⏮️ 🔢 `redoc_url`.
* 👆 💪 ❎ ⚫️ ⚒ `redoc_url=None`.
🖼, ⚒ 🦁 🎚 🍦 `/documentation` &amp; ❎ 📄:
```Python hl_lines="3"
{!../../../docs_src/metadata/tutorial003.py!}
```

View File

@@ -0,0 +1,61 @@
# 🛠️
👆 💪 🚮 🛠️ **FastAPI** 🈸.
"🛠️" 🔢 👈 👷 ⏮️ 🔠 **📨** ⏭ ⚫️ 🛠️ 🙆 🎯 *➡ 🛠️*. &amp; ⏮️ 🔠 **📨** ⏭ 🛬 ⚫️.
* ⚫️ ✊ 🔠 **📨** 👈 👟 👆 🈸.
* ⚫️ 💪 ⤴️ 🕳 👈 **📨** ⚖️ 🏃 🙆 💪 📟.
* ⤴️ ⚫️ 🚶‍♀️ **📨** 🛠️ 🎂 🈸 ( *➡ 🛠️*).
* ⚫️ ⤴️ ✊ **📨** 🏗 🈸 ( *➡ 🛠️*).
* ⚫️ 💪 🕳 👈 **📨** ⚖️ 🏃 🙆 💪 📟.
* ⤴️ ⚫️ 📨 **📨**.
!!! note "📡 "
🚥 👆 ✔️ 🔗 ⏮️ `yield`, 🚪 📟 🔜 🏃 *⏮️* 🛠️.
🚥 📤 🙆 🖥 📋 (📄 ⏪), 👫 🔜 🏃 *⏮️* 🌐 🛠️.
## ✍ 🛠️
✍ 🛠️ 👆 ⚙️ 👨‍🎨 `@app.middleware("http")` 🔛 🔝 🔢.
🛠️ 🔢 📨:
* `request`.
* 🔢 `call_next` 👈 🔜 📨 `request` 🔢.
* 👉 🔢 🔜 🚶‍♀️ `request` 🔗 *➡ 🛠️*.
* ⤴️ ⚫️ 📨 `response` 🏗 🔗 *➡ 🛠️*.
* 👆 💪 ⤴️ 🔀 🌅 `response` ⏭ 🛬 ⚫️.
```Python hl_lines="8-9 11 14"
{!../../../docs_src/middleware/tutorial001.py!}
```
!!! tip
✔️ 🤯 👈 🛃 © 🎚 💪 🚮 <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers" class="external-link" target="_blank">⚙️ '✖-' 🔡</a>.
✋️ 🚥 👆 ✔️ 🛃 🎚 👈 👆 💚 👩‍💻 🖥 💪 👀, 👆 💪 🚮 👫 👆 ⚜ 📳 ([⚜ (✖️-🇨🇳 🤝)](cors.md){.internal-link target=_blank}) ⚙️ 🔢 `expose_headers` 📄 <a href="https://www.starlette.io/middleware/#corsmiddleware" class="external-link" target="_blank">💃 ⚜ 🩺</a>.
!!! note "📡 "
👆 💪 ⚙️ `from starlette.requests import Request`.
**FastAPI** 🚚 ⚫️ 🏪 👆, 👩‍💻. ✋️ ⚫️ 👟 🔗 ⚪️➡️ 💃.
### ⏭ &amp; ⏮️ `response`
👆 💪 🚮 📟 🏃 ⏮️ `request`, ⏭ 🙆 *➡ 🛠️* 📨 ⚫️.
&amp; ⏮️ `response` 🏗, ⏭ 🛬 ⚫️.
🖼, 👆 💪 🚮 🛃 🎚 `X-Process-Time` ⚗ 🕰 🥈 👈 ⚫️ ✊ 🛠️ 📨 &amp; 🏗 📨:
```Python hl_lines="10 12-13"
{!../../../docs_src/middleware/tutorial001.py!}
```
## 🎏 🛠️
👆 💪 ⏪ ✍ 🌖 🔃 🎏 🛠️ [🏧 👩‍💻 🦮: 🏧 🛠️](../advanced/middleware.md){.internal-link target=_blank}.
👆 🔜 ✍ 🔃 ❔ 🍵 <abbr title="Cross-Origin Resource Sharing">⚜</abbr> ⏮️ 🛠️ ⏭ 📄.

View File

@@ -0,0 +1,179 @@
# ➡ 🛠️ 📳
📤 📚 🔢 👈 👆 💪 🚶‍♀️ 👆 *➡ 🛠️ 👨‍🎨* 🔗 ⚫️.
!!! warning
👀 👈 👫 🔢 🚶‍♀️ 🔗 *➡ 🛠️ 👨‍🎨*, 🚫 👆 *➡ 🛠️ 🔢*.
## 📨 👔 📟
👆 💪 🔬 (🇺🇸🔍) `status_code` ⚙️ 📨 👆 *➡ 🛠️*.
👆 💪 🚶‍♀️ 🔗 `int` 📟, 💖 `404`.
✋️ 🚥 👆 🚫 💭 ⚫️❔ 🔠 🔢 📟, 👆 💪 ⚙️ ⌨ 📉 `status`:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="3 17"
{!> ../../../docs_src/path_operation_configuration/tutorial001.py!}
```
=== "🐍 3⃣.9️⃣ &amp; 🔛"
```Python hl_lines="3 17"
{!> ../../../docs_src/path_operation_configuration/tutorial001_py39.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="1 15"
{!> ../../../docs_src/path_operation_configuration/tutorial001_py310.py!}
```
👈 👔 📟 🔜 ⚙️ 📨 &amp; 🔜 🚮 🗄 🔗.
!!! note "📡 "
👆 💪 ⚙️ `from starlette import status`.
**FastAPI** 🚚 🎏 `starlette.status` `fastapi.status` 🏪 👆, 👩‍💻. ✋️ ⚫️ 👟 🔗 ⚪️➡️ 💃.
## 🔖
👆 💪 🚮 🔖 👆 *➡ 🛠️*, 🚶‍♀️ 🔢 `tags` ⏮️ `list` `str` (🛎 1⃣ `str`):
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="17 22 27"
{!> ../../../docs_src/path_operation_configuration/tutorial002.py!}
```
=== "🐍 3⃣.9️⃣ &amp; 🔛"
```Python hl_lines="17 22 27"
{!> ../../../docs_src/path_operation_configuration/tutorial002_py39.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="15 20 25"
{!> ../../../docs_src/path_operation_configuration/tutorial002_py310.py!}
```
👫 🔜 🚮 🗄 🔗 &amp; ⚙️ 🏧 🧾 🔢:
<img src="/img/tutorial/path-operation-configuration/image01.png">
### 🔖 ⏮️ 🔢
🚥 👆 ✔️ 🦏 🈸, 👆 5⃣📆 🔚 🆙 📈 **📚 🔖**, &amp; 👆 🔜 💚 ⚒ 💭 👆 🕧 ⚙️ **🎏 🔖** 🔗 *➡ 🛠️*.
👫 💼, ⚫️ 💪 ⚒ 🔑 🏪 🔖 `Enum`.
**FastAPI** 🐕‍🦺 👈 🎏 🌌 ⏮️ ✅ 🎻:
```Python hl_lines="1 8-10 13 18"
{!../../../docs_src/path_operation_configuration/tutorial002b.py!}
```
## 📄 &amp; 📛
👆 💪 🚮 `summary` &amp; `description`:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="20-21"
{!> ../../../docs_src/path_operation_configuration/tutorial003.py!}
```
=== "🐍 3⃣.9️⃣ &amp; 🔛"
```Python hl_lines="20-21"
{!> ../../../docs_src/path_operation_configuration/tutorial003_py39.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="18-19"
{!> ../../../docs_src/path_operation_configuration/tutorial003_py310.py!}
```
## 📛 ⚪️➡️ #️⃣
📛 😑 📏 &amp; 📔 💗 ⏸, 👆 💪 📣 *➡ 🛠️* 📛 🔢 <abbr title="a multi-line string as the first expression inside a function (not assigned to any variable) used for documentation"> #️⃣ </abbr> &amp; **FastAPI** 🔜 ✍ ⚫️ ⚪️➡️ 📤.
👆 💪 ✍ <a href="https://en.wikipedia.org/wiki/Markdown" class="external-link" target="_blank">✍</a> #️⃣ , ⚫️ 🔜 🔬 &amp; 🖥 ☑ (✊ 🔘 🏧 #️⃣ 📐).
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="19-27"
{!> ../../../docs_src/path_operation_configuration/tutorial004.py!}
```
=== "🐍 3⃣.9️⃣ &amp; 🔛"
```Python hl_lines="19-27"
{!> ../../../docs_src/path_operation_configuration/tutorial004_py39.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="17-25"
{!> ../../../docs_src/path_operation_configuration/tutorial004_py310.py!}
```
⚫️ 🔜 ⚙️ 🎓 🩺:
<img src="/img/tutorial/path-operation-configuration/image02.png">
## 📨 📛
👆 💪 ✔ 📨 📛 ⏮️ 🔢 `response_description`:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="21"
{!> ../../../docs_src/path_operation_configuration/tutorial005.py!}
```
=== "🐍 3⃣.9️⃣ &amp; 🔛"
```Python hl_lines="21"
{!> ../../../docs_src/path_operation_configuration/tutorial005_py39.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="19"
{!> ../../../docs_src/path_operation_configuration/tutorial005_py310.py!}
```
!!! info
👀 👈 `response_description` 🔗 🎯 📨, `description` 🔗 *➡ 🛠️* 🏢.
!!! check
🗄 ✔ 👈 🔠 *➡ 🛠️* 🚚 📨 📛.
, 🚥 👆 🚫 🚚 1⃣, **FastAPI** 🔜 🔁 🏗 1⃣ "🏆 📨".
<img src="/img/tutorial/path-operation-configuration/image03.png">
## 😢 *➡ 🛠️*
🚥 👆 💪 ™ *➡ 🛠️* <abbr title="obsolete, recommended not to use it">😢</abbr>, ✋️ 🍵 ❎ ⚫️, 🚶‍♀️ 🔢 `deprecated`:
```Python hl_lines="16"
{!../../../docs_src/path_operation_configuration/tutorial006.py!}
```
⚫️ 🔜 🎯 ™ 😢 🎓 🩺:
<img src="/img/tutorial/path-operation-configuration/image04.png">
✅ ❔ 😢 &amp; 🚫-😢 *➡ 🛠️* 👀 💖:
<img src="/img/tutorial/path-operation-configuration/image05.png">
## 🌃
👆 💪 🔗 &amp; 🚮 🗃 👆 *➡ 🛠️* 💪 🚶‍♀️ 🔢 *➡ 🛠️ 👨‍🎨*.

View File

@@ -0,0 +1,138 @@
# ➡ 🔢 &amp; 🔢 🔬
🎏 🌌 👈 👆 💪 📣 🌅 🔬 &amp; 🗃 🔢 🔢 ⏮️ `Query`, 👆 💪 📣 🎏 🆎 🔬 &amp; 🗃 ➡ 🔢 ⏮️ `Path`.
## 🗄 ➡
🥇, 🗄 `Path` ⚪️➡️ `fastapi`:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="3"
{!> ../../../docs_src/path_params_numeric_validations/tutorial001.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="1"
{!> ../../../docs_src/path_params_numeric_validations/tutorial001_py310.py!}
```
## 📣 🗃
👆 💪 📣 🌐 🎏 🔢 `Query`.
🖼, 📣 `title` 🗃 💲 ➡ 🔢 `item_id` 👆 💪 🆎:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="10"
{!> ../../../docs_src/path_params_numeric_validations/tutorial001.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="8"
{!> ../../../docs_src/path_params_numeric_validations/tutorial001_py310.py!}
```
!!! note
➡ 🔢 🕧 ✔ ⚫️ ✔️ 🍕 ➡.
, 👆 🔜 📣 ⚫️ ⏮️ `...` ™ ⚫️ ✔.
👐, 🚥 👆 📣 ⚫️ ⏮️ `None` ⚖️ ⚒ 🔢 💲, ⚫️ 🔜 🚫 📉 🕳, ⚫️ 🔜 🕧 🚚.
## ✔ 🔢 👆 💪
➡️ 💬 👈 👆 💚 📣 🔢 🔢 `q` ✔ `str`.
&amp; 👆 🚫 💪 📣 🕳 🙆 👈 🔢, 👆 🚫 🤙 💪 ⚙️ `Query`.
✋️ 👆 💪 ⚙️ `Path` `item_id` ➡ 🔢.
🐍 🔜 😭 🚥 👆 🚮 💲 ⏮️ "🔢" ⏭ 💲 👈 🚫 ✔️ "🔢".
✋️ 👆 💪 🏤-✔ 👫, &amp; ✔️ 💲 🍵 🔢 (🔢 🔢 `q`) 🥇.
⚫️ 🚫 🤔 **FastAPI**. ⚫️ 🔜 🔍 🔢 👫 📛, 🆎 &amp; 🔢 📄 (`Query`, `Path`, ♒️), ⚫️ 🚫 💅 🔃 ✔.
, 👆 💪 📣 👆 🔢:
```Python hl_lines="7"
{!../../../docs_src/path_params_numeric_validations/tutorial002.py!}
```
## ✔ 🔢 👆 💪, 🎱
🚥 👆 💚 📣 `q` 🔢 🔢 🍵 `Query` 🚫 🙆 🔢 💲, &amp; ➡ 🔢 `item_id` ⚙️ `Path`, &amp; ✔️ 👫 🎏 ✔, 🐍 ✔️ 🐥 🎁 ❕ 👈.
🚶‍♀️ `*`, 🥇 🔢 🔢.
🐍 🏆 🚫 🕳 ⏮️ 👈 `*`, ✋️ ⚫️ 🔜 💭 👈 🌐 📄 🔢 🔜 🤙 🇨🇻 ❌ (🔑-💲 👫), 💭 <abbr title="From: K-ey W-ord Arg-uments"><code>kwargs</code></abbr>. 🚥 👫 🚫 ✔️ 🔢 💲.
```Python hl_lines="7"
{!../../../docs_src/path_params_numeric_validations/tutorial003.py!}
```
## 🔢 🔬: 👑 🌘 ⚖️ 🌓
⏮️ `Query` &amp; `Path` (&amp; 🎏 👆 🔜 👀 ⏪) 👆 💪 📣 🔢 ⚛.
📥, ⏮️ `ge=1`, `item_id` 🔜 💪 🔢 🔢 "`g`🅾 🌘 ⚖️ `e`🅾" `1`.
```Python hl_lines="8"
{!../../../docs_src/path_params_numeric_validations/tutorial004.py!}
```
## 🔢 🔬: 🌘 🌘 &amp; 🌘 🌘 ⚖️ 🌓
🎏 ✔:
* `gt`: `g`🅾 `t`👲
* `le`: `l`👭 🌘 ⚖️ `e`🅾
```Python hl_lines="9"
{!../../../docs_src/path_params_numeric_validations/tutorial005.py!}
```
## 🔢 🔬: 🎈, 🌘 🌘 &amp; 🌘 🌘
🔢 🔬 👷 `float` 💲.
📥 🌐❔ ⚫️ ▶️️ ⚠ 💪 📣 <abbr title="greater than"><code>gt</code></abbr> &amp; 🚫 <abbr title="greater than or equal"><code>ge</code></abbr>. ⏮️ ⚫️ 👆 💪 🚚, 🖼, 👈 💲 🔜 👑 🌘 `0`, 🚥 ⚫️ 🌘 🌘 `1`.
, `0.5` 🔜 ☑ 💲. ✋️ `0.0` ⚖️ `0` 🔜 🚫.
&amp; 🎏 <abbr title="less than"><code>lt</code></abbr>.
```Python hl_lines="11"
{!../../../docs_src/path_params_numeric_validations/tutorial006.py!}
```
## 🌃
⏮️ `Query`, `Path` (&amp; 🎏 👆 🚫 👀) 👆 💪 📣 🗃 &amp; 🎻 🔬 🎏 🌌 ⏮️ [🔢 🔢 &amp; 🎻 🔬](query-params-str-validations.md){.internal-link target=_blank}.
&amp; 👆 💪 📣 🔢 🔬:
* `gt`: `g`🅾 `t`👲
* `ge`: `g`🅾 🌘 ⚖️ `e`🅾
* `lt`: `l`👭 `t`👲
* `le`: `l`👭 🌘 ⚖️ `e`🅾
!!! info
`Query`, `Path`, &amp; 🎏 🎓 👆 🔜 👀 ⏪ 🏿 ⚠ `Param` 🎓.
🌐 👫 💰 🎏 🔢 🌖 🔬 &amp; 🗃 👆 ✔️ 👀.
!!! note "📡 "
🕐❔ 👆 🗄 `Query`, `Path` &amp; 🎏 ⚪️➡️ `fastapi`, 👫 🤙 🔢.
👈 🕐❔ 🤙, 📨 👐 🎓 🎏 📛.
, 👆 🗄 `Query`, ❔ 🔢. &amp; 🕐❔ 👆 🤙 ⚫️, ⚫️ 📨 👐 🎓 🌟 `Query`.
👫 🔢 📤 (↩️ ⚙️ 🎓 🔗) 👈 👆 👨‍🎨 🚫 ™ ❌ 🔃 👫 🆎.
👈 🌌 👆 💪 ⚙️ 👆 😐 👨‍🎨 &amp; 🛠️ 🧰 🍵 ✔️ 🚮 🛃 📳 🤷‍♂ 📚 ❌.

View File

@@ -0,0 +1,252 @@
# ➡ 🔢
👆 💪 📣 ➡ "🔢" ⚖️ "🔢" ⏮️ 🎏 ❕ ⚙️ 🐍 📁 🎻:
```Python hl_lines="6-7"
{!../../../docs_src/path_params/tutorial001.py!}
```
💲 ➡ 🔢 `item_id` 🔜 🚶‍♀️ 👆 🔢 ❌ `item_id`.
, 🚥 👆 🏃 👉 🖼 &amp; 🚶 <a href="http://127.0.0.1:8000/items/foo" class="external-link" target="_blank">http://127.0.0.1:8000/items/foo</a>, 👆 🔜 👀 📨:
```JSON
{"item_id":"foo"}
```
## ➡ 🔢 ⏮️ 🆎
👆 💪 📣 🆎 ➡ 🔢 🔢, ⚙️ 🐩 🐍 🆎 ✍:
```Python hl_lines="7"
{!../../../docs_src/path_params/tutorial002.py!}
```
👉 💼, `item_id` 📣 `int`.
!!! check
👉 🔜 🤝 👆 👨‍🎨 🐕‍🦺 🔘 👆 🔢, ⏮️ ❌ ✅, 🛠️, ♒️.
## 💽 <abbr title="also known as: serialization, parsing, marshalling">🛠️</abbr>
🚥 👆 🏃 👉 🖼 &amp; 📂 👆 🖥 <a href="http://127.0.0.1:8000/items/3" class="external-link" target="_blank">http://127.0.0.1:8000/items/3</a>, 👆 🔜 👀 📨:
```JSON
{"item_id":3}
```
!!! check
👀 👈 💲 👆 🔢 📨 (&amp; 📨) `3`, 🐍 `int`, 🚫 🎻 `"3"`.
, ⏮️ 👈 🆎 📄, **FastAPI** 🤝 👆 🏧 📨 <abbr title="converting the string that comes from an HTTP request into Python data">"✍"</abbr>.
## 💽 🔬
✋️ 🚥 👆 🚶 🖥 <a href="http://127.0.0.1:8000/items/foo" class="external-link" target="_blank">http://127.0.0.1:8000/items/foo</a>, 👆 🔜 👀 👌 🇺🇸🔍 ❌:
```JSON
{
"detail": [
{
"loc": [
"path",
"item_id"
],
"msg": "value is not a valid integer",
"type": "type_error.integer"
}
]
}
```
↩️ ➡ 🔢 `item_id` ✔️ 💲 `"foo"`, ❔ 🚫 `int`.
🎏 ❌ 🔜 😑 🚥 👆 🚚 `float` ↩️ `int`,: <a href="http://127.0.0.1:8000/items/4.2" class="external-link" target="_blank">http://127.0.0.1:8000/items/4.2</a>
!!! check
, ⏮️ 🎏 🐍 🆎 📄, **FastAPI** 🤝 👆 💽 🔬.
👀 👈 ❌ 🎯 🇵🇸 ⚫️❔ ☝ 🌐❔ 🔬 🚫 🚶‍♀️.
👉 🙃 👍 ⏪ 🛠️ &amp; 🛠️ 📟 👈 🔗 ⏮️ 👆 🛠️.
## 🧾
&amp; 🕐❔ 👆 📂 👆 🖥 <a href="http://127.0.0.1:8000/docs" class="external-link" target="_blank">http://127.0.0.1:8000/docs</a>, 👆 🔜 👀 🏧, 🎓, 🛠️ 🧾 💖:
<img src="/img/tutorial/path-params/image01.png">
!!! check
🔄, ⏮️ 👈 🎏 🐍 🆎 📄, **FastAPI** 🤝 👆 🏧, 🎓 🧾 (🛠️ 🦁 🎚).
👀 👈 ➡ 🔢 📣 🔢.
## 🐩-⚓️ 💰, 🎛 🧾
&amp; ↩️ 🏗 🔗 ⚪️➡️ <a href="https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md" class="external-link" target="_blank">🗄</a> 🐩, 📤 📚 🔗 🧰.
↩️ 👉, **FastAPI** ⚫️ 🚚 🎛 🛠️ 🧾 (⚙️ 📄), ❔ 👆 💪 🔐 <a href="http://127.0.0.1:8000/redoc" class="external-link" target="_blank">http://127.0.0.1:8000/redoc</a>:
<img src="/img/tutorial/path-params/image02.png">
🎏 🌌, 📤 📚 🔗 🧰. ✅ 📟 ⚡ 🧰 📚 🇪🇸.
## Pydantic
🌐 💽 🔬 🎭 🔽 🚘 <a href="https://pydantic-docs.helpmanual.io/" class="external-link" target="_blank">Pydantic</a>, 👆 🤚 🌐 💰 ⚪️➡️ ⚫️. &amp; 👆 💭 👆 👍 ✋.
👆 💪 ⚙️ 🎏 🆎 📄 ⏮️ `str`, `float`, `bool` &amp; 📚 🎏 🏗 📊 🆎.
📚 👫 🔬 ⏭ 📃 🔰.
## ✔ 🤔
🕐❔ 🏗 *➡ 🛠️*, 👆 💪 🔎 ⚠ 🌐❔ 👆 ✔️ 🔧 ➡.
💖 `/users/me`, ➡️ 💬 👈 ⚫️ 🤚 📊 🔃 ⏮️ 👩‍💻.
&amp; ⤴️ 👆 💪 ✔️ ➡ `/users/{user_id}` 🤚 💽 🔃 🎯 👩‍💻 👩‍💻 🆔.
↩️ *➡ 🛠️* 🔬 ✔, 👆 💪 ⚒ 💭 👈 ➡ `/users/me` 📣 ⏭ 1⃣ `/users/{user_id}`:
```Python hl_lines="6 11"
{!../../../docs_src/path_params/tutorial003.py!}
```
⏪, ➡ `/users/{user_id}` 🔜 🏏 `/users/me`, "💭" 👈 ⚫️ 📨 🔢 `user_id` ⏮️ 💲 `"me"`.
➡, 👆 🚫🔜 ↔ ➡ 🛠️:
```Python hl_lines="6 11"
{!../../../docs_src/path_params/tutorial003b.py!}
```
🥇 🕐 🔜 🕧 ⚙️ ↩️ ➡ 🏏 🥇.
## 🔁 💲
🚥 👆 ✔️ *➡ 🛠️* 👈 📨 *➡ 🔢*, ✋️ 👆 💚 💪 ☑ *➡ 🔢* 💲 🔁, 👆 💪 ⚙️ 🐩 🐍 <abbr title="Enumeration">`Enum`</abbr>.
### ✍ `Enum` 🎓
🗄 `Enum` &amp; ✍ 🎧-🎓 👈 😖 ⚪️➡️ `str` &amp; ⚪️➡️ `Enum`.
😖 ⚪️➡️ `str` 🛠️ 🩺 🔜 💪 💭 👈 💲 🔜 🆎 `string` &amp; 🔜 💪 ✍ ☑.
⤴️ ✍ 🎓 🔢 ⏮️ 🔧 💲, ❔ 🔜 💪 ☑ 💲:
```Python hl_lines="1 6-9"
{!../../../docs_src/path_params/tutorial005.py!}
```
!!! info
<a href="https://docs.python.org/3/library/enum.html" class="external-link" target="_blank">🔢 (⚖️ 🔢) 💪 🐍</a> ↩️ ⏬ 3⃣.4️⃣.
!!! tip
🚥 👆 💭, "📊", "🎓", &amp; "🍏" 📛 🎰 🏫 <abbr title="Technically, Deep Learning model architectures">🏷</abbr>.
### 📣 *➡ 🔢*
⤴️ ✍ *➡ 🔢* ⏮️ 🆎 ✍ ⚙️ 🔢 🎓 👆 ✍ (`ModelName`):
```Python hl_lines="16"
{!../../../docs_src/path_params/tutorial005.py!}
```
### ✅ 🩺
↩️ 💪 💲 *➡ 🔢* 🔢, 🎓 🩺 💪 🎦 👫 🎆:
<img src="/img/tutorial/path-params/image03.png">
### 👷 ⏮️ 🐍 *🔢*
💲 *➡ 🔢* 🔜 *🔢 👨‍🎓*.
#### 🔬 *🔢 👨‍🎓*
👆 💪 🔬 ⚫️ ⏮️ *🔢 👨‍🎓* 👆 ✍ 🔢 `ModelName`:
```Python hl_lines="17"
{!../../../docs_src/path_params/tutorial005.py!}
```
#### 🤚 *🔢 💲*
👆 💪 🤚 ☑ 💲 ( `str` 👉 💼) ⚙️ `model_name.value`, ⚖️ 🏢, `your_enum_member.value`:
```Python hl_lines="20"
{!../../../docs_src/path_params/tutorial005.py!}
```
!!! tip
👆 💪 🔐 💲 `"lenet"` ⏮️ `ModelName.lenet.value`.
#### 📨 *🔢 👨‍🎓*
👆 💪 📨 *🔢 👨‍🎓* ⚪️➡️ 👆 *➡ 🛠️*, 🐦 🎻 💪 (✅ `dict`).
👫 🔜 🗜 👫 🔗 💲 (🎻 👉 💼) ⏭ 🛬 👫 👩‍💻:
```Python hl_lines="18 21 23"
{!../../../docs_src/path_params/tutorial005.py!}
```
👆 👩‍💻 👆 🔜 🤚 🎻 📨 💖:
```JSON
{
"model_name": "alexnet",
"message": "Deep Learning FTW!"
}
```
## ➡ 🔢 ⚗ ➡
➡️ 💬 👆 ✔️ *➡ 🛠️* ⏮️ ➡ `/files/{file_path}`.
✋️ 👆 💪 `file_path` ⚫️ 🔌 *➡*, 💖 `home/johndoe/myfile.txt`.
, 📛 👈 📁 🔜 🕳 💖: `/files/home/johndoe/myfile.txt`.
### 🗄 🐕‍🦺
🗄 🚫 🐕‍🦺 🌌 📣 *➡ 🔢* 🔌 *➡* 🔘, 👈 💪 ↘️ 😐 👈 ⚠ 💯 &amp; 🔬.
👐, 👆 💪 ⚫️ **FastAPI**, ⚙️ 1⃣ 🔗 🧰 ⚪️➡️ 💃.
&amp; 🩺 🔜 👷, 👐 🚫 ❎ 🙆 🧾 💬 👈 🔢 🔜 🔌 ➡.
### ➡ 🔌
⚙️ 🎛 🔗 ⚪️➡️ 💃 👆 💪 📣 *➡ 🔢* ⚗ *➡* ⚙️ 📛 💖:
```
/files/{file_path:path}
```
👉 💼, 📛 🔢 `file_path`, &amp; 🏁 🍕, `:path`, 💬 ⚫️ 👈 🔢 🔜 🏏 🙆 *➡*.
, 👆 💪 ⚙️ ⚫️ ⏮️:
```Python hl_lines="6"
{!../../../docs_src/path_params/tutorial004.py!}
```
!!! tip
👆 💪 💪 🔢 🔌 `/home/johndoe/myfile.txt`, ⏮️ 🏁 🔪 (`/`).
👈 💼, 📛 🔜: `/files//home/johndoe/myfile.txt`, ⏮️ 2⃣✖ 🔪 (`//`) 🖖 `files` &amp; `home`.
## 🌃
⏮️ **FastAPI**, ⚙️ 📏, 🏋️ &amp; 🐩 🐍 🆎 📄, 👆 🤚:
* 👨‍🎨 🐕‍🦺: ❌ ✅, ✍, ♒️.
* 💽 "<abbr title="converting the string that comes from an HTTP request into Python data">✍</abbr>"
* 💽 🔬
* 🛠️ ✍ &amp; 🏧 🧾
&amp; 👆 🕴 ✔️ 📣 👫 🕐.
👈 🎲 👑 ⭐ 📈 **FastAPI** 🔬 🎛 🛠️ (↖️ ⚪️➡️ 🍣 🎭).

View File

@@ -0,0 +1,467 @@
# 🔢 🔢 &amp; 🎻 🔬
**FastAPI** ✔ 👆 📣 🌖 &amp; 🔬 👆 🔢.
➡️ ✊ 👉 🈸 🖼:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="9"
{!> ../../../docs_src/query_params_str_validations/tutorial001.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="7"
{!> ../../../docs_src/query_params_str_validations/tutorial001_py310.py!}
```
🔢 🔢 `q` 🆎 `Union[str, None]` (⚖️ `str | None` 🐍 3⃣.1⃣0⃣), 👈 ⛓ 👈 ⚫️ 🆎 `str` ✋️ 💪 `None`, &amp; 👐, 🔢 💲 `None`, FastAPI 🔜 💭 ⚫️ 🚫 ✔.
!!! note
FastAPI 🔜 💭 👈 💲 `q` 🚫 ✔ ↩️ 🔢 💲 `= None`.
`Union` `Union[str, None]` 🔜 ✔ 👆 👨‍🎨 🤝 👆 👍 🐕‍🦺 &amp; 🔍 ❌.
## 🌖 🔬
👥 🔜 🛠️ 👈 ✋️ `q` 📦, 🕐❔ ⚫️ 🚚, **🚮 📐 🚫 📉 5⃣0⃣ 🦹**.
### 🗄 `Query`
🏆 👈, 🥇 🗄 `Query` ⚪️➡️ `fastapi`:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="3"
{!> ../../../docs_src/query_params_str_validations/tutorial002.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="1"
{!> ../../../docs_src/query_params_str_validations/tutorial002_py310.py!}
```
## ⚙️ `Query` 🔢 💲
&amp; 🔜 ⚙️ ⚫️ 🔢 💲 👆 🔢, ⚒ 🔢 `max_length` 5⃣0⃣:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="9"
{!> ../../../docs_src/query_params_str_validations/tutorial002.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="7"
{!> ../../../docs_src/query_params_str_validations/tutorial002_py310.py!}
```
👥 ✔️ ❎ 🔢 💲 `None` 🔢 ⏮️ `Query()`, 👥 💪 🔜 ⚒ 🔢 💲 ⏮️ 🔢 `Query(default=None)`, ⚫️ 🍦 🎏 🎯 ⚖ 👈 🔢 💲.
:
```Python
q: Union[str, None] = Query(default=None)
```
...⚒ 🔢 📦, 🎏:
```Python
q: Union[str, None] = None
```
&amp; 🐍 3⃣.1⃣0⃣ &amp; 🔛:
```Python
q: str | None = Query(default=None)
```
...⚒ 🔢 📦, 🎏:
```Python
q: str | None = None
```
✋️ ⚫️ 📣 ⚫️ 🎯 💆‍♂ 🔢 🔢.
!!! info
✔️ 🤯 👈 🌅 ⚠ 🍕 ⚒ 🔢 📦 🍕:
```Python
= None
```
⚖️:
```Python
= Query(default=None)
```
⚫️ 🔜 ⚙️ 👈 `None` 🔢 💲, &amp; 👈 🌌 ⚒ 🔢 **🚫 ✔**.
`Union[str, None]` 🍕 ✔ 👆 👨‍🎨 🚚 👻 🐕‍🦺, ✋️ ⚫️ 🚫 ⚫️❔ 💬 FastAPI 👈 👉 🔢 🚫 ✔.
⤴️, 👥 💪 🚶‍♀️ 🌅 🔢 `Query`. 👉 💼, `max_length` 🔢 👈 ✔ 🎻:
```Python
q: Union[str, None] = Query(default=None, max_length=50)
```
👉 🔜 ✔ 📊, 🎦 🆑 ❌ 🕐❔ 📊 🚫 ☑, &amp; 📄 🔢 🗄 🔗 *➡ 🛠️*.
## 🚮 🌅 🔬
👆 💪 🚮 🔢 `min_length`:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="10"
{!> ../../../docs_src/query_params_str_validations/tutorial003.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="7"
{!> ../../../docs_src/query_params_str_validations/tutorial003_py310.py!}
```
## 🚮 🥔 🧬
👆 💪 🔬 <abbr title="A regular expression, regex or regexp is a sequence of characters that define a search pattern for strings.">🥔 🧬</abbr> 👈 🔢 🔜 🏏:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="11"
{!> ../../../docs_src/query_params_str_validations/tutorial004.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="9"
{!> ../../../docs_src/query_params_str_validations/tutorial004_py310.py!}
```
👉 🎯 🥔 🧬 ✅ 👈 📨 🔢 💲:
* `^`: ▶️ ⏮️ 📄 🦹, 🚫 ✔️ 🦹 ⏭.
* `fixedquery`: ✔️ ☑ 💲 `fixedquery`.
* `$`: 🔚 📤, 🚫 ✔️ 🙆 🌖 🦹 ⏮️ `fixedquery`.
🚥 👆 💭 💸 ⏮️ 🌐 👉 **"🥔 🧬"** 💭, 🚫 😟. 👫 🏋️ ❔ 📚 👫👫. 👆 💪 📚 💩 🍵 💆‍♂ 🥔 🧬.
✋️ 🕐❔ 👆 💪 👫 &amp; 🚶 &amp; 💡 👫, 💭 👈 👆 💪 ⏪ ⚙️ 👫 🔗 **FastAPI**.
## 🔢 💲
🎏 🌌 👈 👆 💪 🚶‍♀️ `None` 💲 `default` 🔢, 👆 💪 🚶‍♀️ 🎏 💲.
➡️ 💬 👈 👆 💚 📣 `q` 🔢 🔢 ✔️ `min_length` `3`, &amp; ✔️ 🔢 💲 `"fixedquery"`:
```Python hl_lines="7"
{!../../../docs_src/query_params_str_validations/tutorial005.py!}
```
!!! note
✔️ 🔢 💲 ⚒ 🔢 📦.
## ⚒ ⚫️ ✔
🕐❔ 👥 🚫 💪 📣 🌅 🔬 ⚖️ 🗃, 👥 💪 ⚒ `q` 🔢 🔢 ✔ 🚫 📣 🔢 💲, 💖:
```Python
q: str
```
↩️:
```Python
q: Union[str, None] = None
```
✋️ 👥 🔜 📣 ⚫️ ⏮️ `Query`, 🖼 💖:
```Python
q: Union[str, None] = Query(default=None, min_length=3)
```
, 🕐❔ 👆 💪 📣 💲 ✔ ⏪ ⚙️ `Query`, 👆 💪 🎯 🚫 📣 🔢 💲:
```Python hl_lines="7"
{!../../../docs_src/query_params_str_validations/tutorial006.py!}
```
### ✔ ⏮️ ❕ (`...`)
📤 🎛 🌌 🎯 📣 👈 💲 ✔. 👆 💪 ⚒ `default` 🔢 🔑 💲 `...`:
```Python hl_lines="7"
{!../../../docs_src/query_params_str_validations/tutorial006b.py!}
```
!!! info
🚥 👆 🚫 👀 👈 `...` ⏭: ⚫️ 🎁 👁 💲, ⚫️ <a href="https://docs.python.org/3/library/constants.html#Ellipsis" class="external-link" target="_blank">🍕 🐍 &amp; 🤙 "❕"</a>.
⚫️ ⚙️ Pydantic &amp; FastAPI 🎯 📣 👈 💲 ✔.
👉 🔜 ➡️ **FastAPI** 💭 👈 👉 🔢 ✔.
### ✔ ⏮️ `None`
👆 💪 📣 👈 🔢 💪 🚫 `None`, ✋️ 👈 ⚫️ ✔. 👉 🔜 ⚡ 👩‍💻 📨 💲, 🚥 💲 `None`.
👈, 👆 💪 📣 👈 `None` ☑ 🆎 ✋️ ⚙️ `default=...`:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="9"
{!> ../../../docs_src/query_params_str_validations/tutorial006c.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="7"
{!> ../../../docs_src/query_params_str_validations/tutorial006c_py310.py!}
```
!!! tip
Pydantic, ❔ ⚫️❔ 🏋️ 🌐 💽 🔬 &amp; 🛠️ FastAPI, ✔️ 🎁 🎭 🕐❔ 👆 ⚙️ `Optional` ⚖️ `Union[Something, None]` 🍵 🔢 💲, 👆 💪 ✍ 🌅 🔃 ⚫️ Pydantic 🩺 🔃 <a href="https://pydantic-docs.helpmanual.io/usage/models/#required-optional-fields" class="external-link" target="_blank">✔ 📦 🏑</a>.
### ⚙️ Pydantic `Required` ↩️ ❕ (`...`)
🚥 👆 💭 😬 ⚙️ `...`, 👆 💪 🗄 &amp; ⚙️ `Required` ⚪️➡️ Pydantic:
```Python hl_lines="2 8"
{!../../../docs_src/query_params_str_validations/tutorial006d.py!}
```
!!! tip
💭 👈 🌅 💼, 🕐❔ 🕳 🚚, 👆 💪 🎯 🚫 `default` 🔢, 👆 🛎 🚫 ✔️ ⚙️ `...` 🚫 `Required`.
## 🔢 🔢 📇 / 💗 💲
🕐❔ 👆 🔬 🔢 🔢 🎯 ⏮️ `Query` 👆 💪 📣 ⚫️ 📨 📇 💲, ⚖️ 🙆‍♀ 🎏 🌌, 📨 💗 💲.
🖼, 📣 🔢 🔢 `q` 👈 💪 😑 💗 🕰 📛, 👆 💪 ✍:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="9"
{!> ../../../docs_src/query_params_str_validations/tutorial011.py!}
```
=== "🐍 3⃣.9️⃣ &amp; 🔛"
```Python hl_lines="9"
{!> ../../../docs_src/query_params_str_validations/tutorial011_py39.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="7"
{!> ../../../docs_src/query_params_str_validations/tutorial011_py310.py!}
```
⤴️, ⏮️ 📛 💖:
```
http://localhost:8000/items/?q=foo&q=bar
```
👆 🔜 📨 💗 `q` *🔢 🔢'* 💲 (`foo` &amp; `bar`) 🐍 `list` 🔘 👆 *➡ 🛠️ 🔢*, *🔢 🔢* `q`.
, 📨 👈 📛 🔜:
```JSON
{
"q": [
"foo",
"bar"
]
}
```
!!! tip
📣 🔢 🔢 ⏮️ 🆎 `list`, 💖 🖼 🔛, 👆 💪 🎯 ⚙️ `Query`, ⏪ ⚫️ 🔜 🔬 📨 💪.
🎓 🛠️ 🩺 🔜 ➡️, ✔ 💗 💲:
<img src="/img/tutorial/query-params-str-validations/image02.png">
### 🔢 🔢 📇 / 💗 💲 ⏮️ 🔢
&amp; 👆 💪 🔬 🔢 `list` 💲 🚥 👌 🚚:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="9"
{!> ../../../docs_src/query_params_str_validations/tutorial012.py!}
```
=== "🐍 3⃣.9️⃣ &amp; 🔛"
```Python hl_lines="7"
{!> ../../../docs_src/query_params_str_validations/tutorial012_py39.py!}
```
🚥 👆 🚶:
```
http://localhost:8000/items/
```
🔢 `q` 🔜: `["foo", "bar"]` &amp; 👆 📨 🔜:
```JSON
{
"q": [
"foo",
"bar"
]
}
```
#### ⚙️ `list`
👆 💪 ⚙️ `list` 🔗 ↩️ `List[str]` (⚖️ `list[str]` 🐍 3⃣.9️⃣ ):
```Python hl_lines="7"
{!../../../docs_src/query_params_str_validations/tutorial013.py!}
```
!!! note
✔️ 🤯 👈 👉 💼, FastAPI 🏆 🚫 ✅ 🎚 📇.
🖼, `List[int]` 🔜 ✅ (&amp; 📄) 👈 🎚 📇 🔢. ✋️ `list` 😞 🚫🔜.
## 📣 🌅 🗃
👆 💪 🚮 🌅 🔃 🔢.
👈 🔜 🔌 🏗 🗄 &amp; ⚙️ 🧾 👩‍💻 🔢 &amp; 🔢 🧰.
!!! note
✔️ 🤯 👈 🎏 🧰 5⃣📆 ✔️ 🎏 🎚 🗄 🐕‍🦺.
👫 💪 🚫 🎦 🌐 📣, 👐 🌅 💼, ❌ ⚒ ⏪ 📄 🛠️.
👆 💪 🚮 `title`:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="10"
{!> ../../../docs_src/query_params_str_validations/tutorial007.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="8"
{!> ../../../docs_src/query_params_str_validations/tutorial007_py310.py!}
```
&amp; `description`:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="13"
{!> ../../../docs_src/query_params_str_validations/tutorial008.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="12"
{!> ../../../docs_src/query_params_str_validations/tutorial008_py310.py!}
```
## 📛 🔢
🌈 👈 👆 💚 🔢 `item-query`.
💖:
```
http://127.0.0.1:8000/items/?item-query=foobaritems
```
✋️ `item-query` 🚫 ☑ 🐍 🔢 📛.
🔐 🔜 `item_query`.
✋️ 👆 💪 ⚫️ ⚫️❔ `item-query`...
⤴️ 👆 💪 📣 `alias`, &amp; 👈 📛 ⚫️❔ 🔜 ⚙️ 🔎 🔢 💲:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="9"
{!> ../../../docs_src/query_params_str_validations/tutorial009.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="7"
{!> ../../../docs_src/query_params_str_validations/tutorial009_py310.py!}
```
## 😛 🔢
🔜 ➡️ 💬 👆 🚫 💖 👉 🔢 🚫🔜.
👆 ✔️ 👈 ⚫️ 📤 ⏪ ↩️ 📤 👩‍💻 ⚙️ ⚫️, ✋️ 👆 💚 🩺 🎯 🎦 ⚫️ <abbr title="obsolete, recommended not to use it">😢</abbr>.
⤴️ 🚶‍♀️ 🔢 `deprecated=True` `Query`:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="18"
{!> ../../../docs_src/query_params_str_validations/tutorial010.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="17"
{!> ../../../docs_src/query_params_str_validations/tutorial010_py310.py!}
```
🩺 🔜 🎦 ⚫️ 💖 👉:
<img src="/img/tutorial/query-params-str-validations/image01.png">
## 🚫 ⚪️➡️ 🗄
🚫 🔢 🔢 ⚪️➡️ 🏗 🗄 🔗 (&amp; ➡️, ⚪️➡️ 🏧 🧾 ⚙️), ⚒ 🔢 `include_in_schema` `Query` `False`:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="10"
{!> ../../../docs_src/query_params_str_validations/tutorial014.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="8"
{!> ../../../docs_src/query_params_str_validations/tutorial014_py310.py!}
```
## 🌃
👆 💪 📣 🌖 🔬 &amp; 🗃 👆 🔢.
💊 🔬 &amp; 🗃:
* `alias`
* `title`
* `description`
* `deprecated`
🔬 🎯 🎻:
* `min_length`
* `max_length`
* `regex`
👫 🖼 👆 👀 ❔ 📣 🔬 `str` 💲.
👀 ⏭ 📃 👀 ❔ 📣 🔬 🎏 🆎, 💖 🔢.

View File

@@ -0,0 +1,225 @@
# 🔢 🔢
🕐❔ 👆 📣 🎏 🔢 🔢 👈 🚫 🍕 ➡ 🔢, 👫 🔁 🔬 "🔢" 🔢.
```Python hl_lines="9"
{!../../../docs_src/query_params/tutorial001.py!}
```
🔢 ⚒ 🔑-💲 👫 👈 🚶 ⏮️ `?` 📛, 🎏 `&` 🦹.
🖼, 📛:
```
http://127.0.0.1:8000/items/?skip=0&limit=10
```
...🔢 🔢:
* `skip`: ⏮️ 💲 `0`
* `limit`: ⏮️ 💲 `10`
👫 🍕 📛, 👫 "🛎" 🎻.
✋️ 🕐❔ 👆 📣 👫 ⏮️ 🐍 🆎 (🖼 🔛, `int`), 👫 🗜 👈 🆎 &amp; ✔ 🛡 ⚫️.
🌐 🎏 🛠️ 👈 ⚖ ➡ 🔢 ✔ 🔢 🔢:
* 👨‍🎨 🐕‍🦺 (🎲)
* 💽 <abbr title="converting the string that comes from an HTTP request into Python data">"✍"</abbr>
* 💽 🔬
* 🏧 🧾
## 🔢
🔢 🔢 🚫 🔧 🍕 ➡, 👫 💪 📦 &amp; 💪 ✔️ 🔢 💲.
🖼 🔛 👫 ✔️ 🔢 💲 `skip=0` &amp; `limit=10`.
, 🔜 📛:
```
http://127.0.0.1:8000/items/
```
🔜 🎏 🔜:
```
http://127.0.0.1:8000/items/?skip=0&limit=10
```
✋️ 🚥 👆 🚶, 🖼:
```
http://127.0.0.1:8000/items/?skip=20
```
🔢 💲 👆 🔢 🔜:
* `skip=20`: ↩️ 👆 ⚒ ⚫️ 📛
* `limit=10`: ↩️ 👈 🔢 💲
## 📦 🔢
🎏 🌌, 👆 💪 📣 📦 🔢 🔢, ⚒ 👫 🔢 `None`:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="9"
{!> ../../../docs_src/query_params/tutorial002.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="7"
{!> ../../../docs_src/query_params/tutorial002_py310.py!}
```
👉 💼, 🔢 🔢 `q` 🔜 📦, &amp; 🔜 `None` 🔢.
!!! check
👀 👈 **FastAPI** 🙃 🥃 👀 👈 ➡ 🔢 `item_id` ➡ 🔢 &amp; `q` 🚫,, ⚫️ 🔢 🔢.
## 🔢 🔢 🆎 🛠️
👆 💪 📣 `bool` 🆎, &amp; 👫 🔜 🗜:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="9"
{!> ../../../docs_src/query_params/tutorial003.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="7"
{!> ../../../docs_src/query_params/tutorial003_py310.py!}
```
👉 💼, 🚥 👆 🚶:
```
http://127.0.0.1:8000/items/foo?short=1
```
⚖️
```
http://127.0.0.1:8000/items/foo?short=True
```
⚖️
```
http://127.0.0.1:8000/items/foo?short=true
```
⚖️
```
http://127.0.0.1:8000/items/foo?short=on
```
⚖️
```
http://127.0.0.1:8000/items/foo?short=yes
```
⚖️ 🙆 🎏 💼 📈 (🔠, 🥇 🔤 🔠, ♒️), 👆 🔢 🔜 👀 🔢 `short` ⏮️ `bool` 💲 `True`. ⏪ `False`.
## 💗 ➡ &amp; 🔢 🔢
👆 💪 📣 💗 ➡ 🔢 &amp; 🔢 🔢 🎏 🕰, **FastAPI** 💭 ❔ ❔.
&amp; 👆 🚫 ✔️ 📣 👫 🙆 🎯 ✔.
👫 🔜 🔬 📛:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="8 10"
{!> ../../../docs_src/query_params/tutorial004.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="6 8"
{!> ../../../docs_src/query_params/tutorial004_py310.py!}
```
## ✔ 🔢 🔢
🕐❔ 👆 📣 🔢 💲 🚫-➡ 🔢 (🔜, 👥 ✔️ 🕴 👀 🔢 🔢), ⤴️ ⚫️ 🚫 ✔.
🚥 👆 🚫 💚 🚮 🎯 💲 ✋️ ⚒ ⚫️ 📦, ⚒ 🔢 `None`.
✋️ 🕐❔ 👆 💚 ⚒ 🔢 🔢 ✔, 👆 💪 🚫 📣 🙆 🔢 💲:
```Python hl_lines="6-7"
{!../../../docs_src/query_params/tutorial005.py!}
```
📥 🔢 🔢 `needy` ✔ 🔢 🔢 🆎 `str`.
🚥 👆 📂 👆 🖥 📛 💖:
```
http://127.0.0.1:8000/items/foo-item
```
...🍵 ❎ ✔ 🔢 `needy`, 👆 🔜 👀 ❌ 💖:
```JSON
{
"detail": [
{
"loc": [
"query",
"needy"
],
"msg": "field required",
"type": "value_error.missing"
}
]
}
```
`needy` 🚚 🔢, 👆 🔜 💪 ⚒ ⚫️ 📛:
```
http://127.0.0.1:8000/items/foo-item?needy=sooooneedy
```
...👉 🔜 👷:
```JSON
{
"item_id": "foo-item",
"needy": "sooooneedy"
}
```
&amp; ↗️, 👆 💪 🔬 🔢 ✔, ✔️ 🔢 💲, &amp; 🍕 📦:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="10"
{!> ../../../docs_src/query_params/tutorial006.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="8"
{!> ../../../docs_src/query_params/tutorial006_py310.py!}
```
👉 💼, 📤 3⃣ 🔢 🔢:
* `needy`, ✔ `str`.
* `skip`, `int` ⏮️ 🔢 💲 `0`.
* `limit`, 📦 `int`.
!!! tip
👆 💪 ⚙️ `Enum`Ⓜ 🎏 🌌 ⏮️ [➡ 🔢](path-params.md#predefined-values){.internal-link target=_blank}.

View File

@@ -0,0 +1,186 @@
# 📨 📁
👆 💪 🔬 📁 📂 👩‍💻 ⚙️ `File`.
!!! info
📨 📂 📁, 🥇 ❎ <a href="https://andrew-d.github.io/python-multipart/" class="external-link" target="_blank">`python-multipart`</a>.
🤶 Ⓜ. `pip install python-multipart`.
👉 ↩️ 📂 📁 📨 "📨 💽".
## 🗄 `File`
🗄 `File` &amp; `UploadFile` ⚪️➡️ `fastapi`:
```Python hl_lines="1"
{!../../../docs_src/request_files/tutorial001.py!}
```
## 🔬 `File` 🔢
✍ 📁 🔢 🎏 🌌 👆 🔜 `Body` ⚖️ `Form`:
```Python hl_lines="7"
{!../../../docs_src/request_files/tutorial001.py!}
```
!!! info
`File` 🎓 👈 😖 🔗 ⚪️➡️ `Form`.
✋️ 💭 👈 🕐❔ 👆 🗄 `Query`, `Path`, `File` &amp; 🎏 ⚪️➡️ `fastapi`, 👈 🤙 🔢 👈 📨 🎁 🎓.
!!! tip
📣 📁 💪, 👆 💪 ⚙️ `File`, ↩️ ⏪ 🔢 🔜 🔬 🔢 🔢 ⚖️ 💪 (🎻) 🔢.
📁 🔜 📂 "📨 💽".
🚥 👆 📣 🆎 👆 *➡ 🛠️ 🔢* 🔢 `bytes`, **FastAPI** 🔜 ✍ 📁 👆 &amp; 👆 🔜 📨 🎚 `bytes`.
✔️ 🤯 👈 👉 ⛓ 👈 🎂 🎚 🔜 🏪 💾. 👉 🔜 👷 👍 🤪 📁.
✋️ 📤 📚 💼 ❔ 👆 💪 💰 ⚪️➡️ ⚙️ `UploadFile`.
## 📁 🔢 ⏮️ `UploadFile`
🔬 📁 🔢 ⏮️ 🆎 `UploadFile`:
```Python hl_lines="12"
{!../../../docs_src/request_files/tutorial001.py!}
```
⚙️ `UploadFile` ✔️ 📚 📈 🤭 `bytes`:
* 👆 🚫 ✔️ ⚙️ `File()` 🔢 💲 🔢.
* ⚫️ ⚙️ "🧵" 📁:
* 📁 🏪 💾 🆙 🔆 📐 📉, &amp; ⏮️ 🚶‍♀️ 👉 📉 ⚫️ 🔜 🏪 💾.
* 👉 ⛓ 👈 ⚫️ 🔜 👷 👍 ⭕ 📁 💖 🖼, 📹, ⭕ 💱, ♒️. 🍵 😩 🌐 💾.
* 👆 💪 🤚 🗃 ⚪️➡️ 📂 📁.
* ⚫️ ✔️ <a href="https://docs.python.org/3/glossary.html#term-file-like-object" class="external-link" target="_blank">📁-💖</a> `async` 🔢.
* ⚫️ 🎦 ☑ 🐍 <a href="https://docs.python.org/3/library/tempfile.html#tempfile.SpooledTemporaryFile" class="external-link" target="_blank">`SpooledTemporaryFile`</a> 🎚 👈 👆 💪 🚶‍♀️ 🔗 🎏 🗃 👈 ⌛ 📁-💖 🎚.
### `UploadFile`
`UploadFile` ✔️ 📄 🔢:
* `filename`: `str` ⏮️ ⏮️ 📁 📛 👈 📂 (✅ `myimage.jpg`).
* `content_type`: `str` ⏮️ 🎚 🆎 (📁 🆎 / 📻 🆎) (✅ `image/jpeg`).
* `file`: <a href="https://docs.python.org/3/library/tempfile.html#tempfile.SpooledTemporaryFile" class="external-link" target="_blank">`SpooledTemporaryFile`</a> ( <a href="https://docs.python.org/3/glossary.html#term-file-like-object" class="external-link" target="_blank">📁-💖</a> 🎚). 👉 ☑ 🐍 📁 👈 👆 💪 🚶‍♀️ 🔗 🎏 🔢 ⚖️ 🗃 👈 ⌛ "📁-💖" 🎚.
`UploadFile` ✔️ 📄 `async` 👩‍🔬. 👫 🌐 🤙 🔗 📁 👩‍🔬 🔘 (⚙️ 🔗 `SpooledTemporaryFile`).
* `write(data)`: ✍ `data` (`str` ⚖️ `bytes`) 📁.
* `read(size)`: ✍ `size` (`int`) 🔢/🦹 📁.
* `seek(offset)`: 🚶 🔢 🧘 `offset` (`int`) 📁.
* 🤶 Ⓜ., `await myfile.seek(0)` 🔜 🚶 ▶️ 📁.
* 👉 ✴️ ⚠ 🚥 👆 🏃 `await myfile.read()` 🕐 &amp; ⤴️ 💪 ✍ 🎚 🔄.
* `close()`: 🔐 📁.
🌐 👫 👩‍🔬 `async` 👩‍🔬, 👆 💪 "⌛" 👫.
🖼, 🔘 `async` *➡ 🛠️ 🔢* 👆 💪 🤚 🎚 ⏮️:
```Python
contents = await myfile.read()
```
🚥 👆 🔘 😐 `def` *➡ 🛠️ 🔢*, 👆 💪 🔐 `UploadFile.file` 🔗, 🖼:
```Python
contents = myfile.file.read()
```
!!! note "`async` 📡 "
🕐❔ 👆 ⚙️ `async` 👩‍🔬, **FastAPI** 🏃 📁 👩‍🔬 🧵 &amp; ⌛ 👫.
!!! note "💃 📡 "
**FastAPI**'Ⓜ `UploadFile` 😖 🔗 ⚪️➡️ **💃**'Ⓜ `UploadFile`, ✋️ 🚮 💪 🍕 ⚒ ⚫️ 🔗 ⏮️ **Pydantic** &amp; 🎏 🍕 FastAPI.
## ⚫️❔ "📨 💽"
🌌 🕸 📨 (`<form></form>`) 📨 💽 💽 🛎 ⚙️ "🎁" 🔢 👈 📊, ⚫️ 🎏 ⚪️➡️ 🎻.
**FastAPI** 🔜 ⚒ 💭 ✍ 👈 📊 ⚪️➡️ ▶️️ 🥉 ↩️ 🎻.
!!! note "📡 "
📊 ⚪️➡️ 📨 🛎 🗜 ⚙️ "📻 🆎" `application/x-www-form-urlencoded` 🕐❔ ⚫️ 🚫 🔌 📁.
✋️ 🕐❔ 📨 🔌 📁, ⚫️ 🗜 `multipart/form-data`. 🚥 👆 ⚙️ `File`, **FastAPI** 🔜 💭 ⚫️ ✔️ 🤚 📁 ⚪️➡️ ☑ 🍕 💪.
🚥 👆 💚 ✍ 🌖 🔃 👉 🔢 &amp; 📨 🏑, 👳 <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST" class="external-link" target="_blank"><abbr title="Mozilla Developer Network">🏇</abbr> 🕸 🩺 <code>POST</code></a>.
!!! warning
👆 💪 📣 💗 `File` &amp; `Form` 🔢 *➡ 🛠️*, ✋️ 👆 💪 🚫 📣 `Body` 🏑 👈 👆 ⌛ 📨 🎻, 📨 🔜 ✔️ 💪 🗜 ⚙️ `multipart/form-data` ↩️ `application/json`.
👉 🚫 🚫 **FastAPI**, ⚫️ 🍕 🇺🇸🔍 🛠️.
## 📦 📁 📂
👆 💪 ⚒ 📁 📦 ⚙️ 🐩 🆎 ✍ &amp; ⚒ 🔢 💲 `None`:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="9 17"
{!> ../../../docs_src/request_files/tutorial001_02.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="7 14"
{!> ../../../docs_src/request_files/tutorial001_02_py310.py!}
```
## `UploadFile` ⏮️ 🌖 🗃
👆 💪 ⚙️ `File()` ⏮️ `UploadFile`, 🖼, ⚒ 🌖 🗃:
```Python hl_lines="13"
{!../../../docs_src/request_files/tutorial001_03.py!}
```
## 💗 📁 📂
⚫️ 💪 📂 📚 📁 🎏 🕰.
👫 🔜 👨‍💼 🎏 "📨 🏑" 📨 ⚙️ "📨 💽".
⚙️ 👈, 📣 📇 `bytes` ⚖️ `UploadFile`:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="10 15"
{!> ../../../docs_src/request_files/tutorial002.py!}
```
=== "🐍 3⃣.9️⃣ &amp; 🔛"
```Python hl_lines="8 13"
{!> ../../../docs_src/request_files/tutorial002_py39.py!}
```
👆 🔜 📨, 📣, `list` `bytes` ⚖️ `UploadFile`Ⓜ.
!!! note "📡 "
👆 💪 ⚙️ `from starlette.responses import HTMLResponse`.
**FastAPI** 🚚 🎏 `starlette.responses` `fastapi.responses` 🏪 👆, 👩‍💻. ✋️ 🌅 💪 📨 👟 🔗 ⚪️➡️ 💃.
### 💗 📁 📂 ⏮️ 🌖 🗃
&amp; 🎏 🌌 ⏭, 👆 💪 ⚙️ `File()` ⚒ 🌖 🔢, `UploadFile`:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="18"
{!> ../../../docs_src/request_files/tutorial003.py!}
```
=== "🐍 3⃣.9️⃣ &amp; 🔛"
```Python hl_lines="16"
{!> ../../../docs_src/request_files/tutorial003_py39.py!}
```
## 🌃
⚙️ `File`, `bytes`, &amp; `UploadFile` 📣 📁 📂 📨, 📨 📨 💽.

View File

@@ -0,0 +1,35 @@
# 📨 📨 &amp; 📁
👆 💪 🔬 📁 &amp; 📨 🏑 🎏 🕰 ⚙️ `File` &amp; `Form`.
!!! info
📨 📂 📁 &amp; /⚖️ 📨 📊, 🥇 ❎ <a href="https://andrew-d.github.io/python-multipart/" class="external-link" target="_blank">`python-multipart`</a>.
🤶 Ⓜ. `pip install python-multipart`.
## 🗄 `File` &amp; `Form`
```Python hl_lines="1"
{!../../../docs_src/request_forms_and_files/tutorial001.py!}
```
## 🔬 `File` &amp; `Form` 🔢
✍ 📁 &amp; 📨 🔢 🎏 🌌 👆 🔜 `Body` ⚖️ `Query`:
```Python hl_lines="8"
{!../../../docs_src/request_forms_and_files/tutorial001.py!}
```
📁 &amp; 📨 🏑 🔜 📂 📨 📊 &amp; 👆 🔜 📨 📁 &amp; 📨 🏑.
&amp; 👆 💪 📣 📁 `bytes` &amp; `UploadFile`.
!!! warning
👆 💪 📣 💗 `File` &amp; `Form` 🔢 *➡ 🛠️*, ✋️ 👆 💪 🚫 📣 `Body` 🏑 👈 👆 ⌛ 📨 🎻, 📨 🔜 ✔️ 💪 🗜 ⚙️ `multipart/form-data` ↩️ `application/json`.
👉 🚫 🚫 **FastAPI**, ⚫️ 🍕 🇺🇸🔍 🛠️.
## 🌃
⚙️ `File` &amp; `Form` 👯‍♂️ 🕐❔ 👆 💪 📨 💽 &amp; 📁 🎏 📨.

View File

@@ -0,0 +1,58 @@
# 📨 💽
🕐❔ 👆 💪 📨 📨 🏑 ↩️ 🎻, 👆 💪 ⚙️ `Form`.
!!! info
⚙️ 📨, 🥇 ❎ <a href="https://andrew-d.github.io/python-multipart/" class="external-link" target="_blank">`python-multipart`</a>.
🤶 Ⓜ. `pip install python-multipart`.
## 🗄 `Form`
🗄 `Form` ⚪️➡️ `fastapi`:
```Python hl_lines="1"
{!../../../docs_src/request_forms/tutorial001.py!}
```
## 🔬 `Form` 🔢
✍ 📨 🔢 🎏 🌌 👆 🔜 `Body` ⚖️ `Query`:
```Python hl_lines="7"
{!../../../docs_src/request_forms/tutorial001.py!}
```
🖼, 1⃣ 🌌 Oauth2⃣ 🔧 💪 ⚙️ (🤙 "🔐 💧") ⚫️ ✔ 📨 `username` &amp; `password` 📨 🏑.
<abbr title="specification">🔌</abbr> 🚚 🏑 ⚫️❔ 📛 `username` &amp; `password`, &amp; 📨 📨 🏑, 🚫 🎻.
⏮️ `Form` 👆 💪 📣 🎏 📳 ⏮️ `Body` (&amp; `Query`, `Path`, `Cookie`), 🔌 🔬, 🖼, 📛 (✅ `user-name` ↩️ `username`), ♒️.
!!! info
`Form` 🎓 👈 😖 🔗 ⚪️➡️ `Body`.
!!! tip
📣 📨 💪, 👆 💪 ⚙️ `Form` 🎯, ↩️ 🍵 ⚫️ 🔢 🔜 🔬 🔢 🔢 ⚖️ 💪 (🎻) 🔢.
## 🔃 "📨 🏑"
🌌 🕸 📨 (`<form></form>`) 📨 💽 💽 🛎 ⚙️ "🎁" 🔢 👈 📊, ⚫️ 🎏 ⚪️➡️ 🎻.
**FastAPI** 🔜 ⚒ 💭 ✍ 👈 📊 ⚪️➡️ ▶️️ 🥉 ↩️ 🎻.
!!! note "📡 "
📊 ⚪️➡️ 📨 🛎 🗜 ⚙️ "📻 🆎" `application/x-www-form-urlencoded`.
✋️ 🕐❔ 📨 🔌 📁, ⚫️ 🗜 `multipart/form-data`. 👆 🔜 ✍ 🔃 🚚 📁 ⏭ 📃.
🚥 👆 💚 ✍ 🌖 🔃 👉 🔢 &amp; 📨 🏑, 👳 <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST" class="external-link" target="_blank"><abbr title="Mozilla Developer Network">🏇</abbr> 🕸 🩺 <code>POST</code></a>.
!!! warning
👆 💪 📣 💗 `Form` 🔢 *➡ 🛠️*, ✋️ 👆 💪 🚫 📣 `Body` 🏑 👈 👆 ⌛ 📨 🎻, 📨 🔜 ✔️ 💪 🗜 ⚙️ `application/x-www-form-urlencoded` ↩️ `application/json`.
👉 🚫 🚫 **FastAPI**, ⚫️ 🍕 🇺🇸🔍 🛠️.
## 🌃
⚙️ `Form` 📣 📨 💽 🔢 🔢.

View File

@@ -0,0 +1,481 @@
# 📨 🏷 - 📨 🆎
👆 💪 📣 🆎 ⚙️ 📨 ✍ *➡ 🛠️ 🔢* **📨 🆎**.
👆 💪 ⚙️ **🆎 ✍** 🎏 🌌 👆 🔜 🔢 💽 🔢 **🔢**, 👆 💪 ⚙️ Pydantic 🏷, 📇, 📖, 📊 💲 💖 🔢, 🎻, ♒️.
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="18 23"
{!> ../../../docs_src/response_model/tutorial001_01.py!}
```
=== "🐍 3⃣.9️⃣ &amp; 🔛"
```Python hl_lines="18 23"
{!> ../../../docs_src/response_model/tutorial001_01_py39.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="16 21"
{!> ../../../docs_src/response_model/tutorial001_01_py310.py!}
```
FastAPI 🔜 ⚙️ 👉 📨 🆎:
* **✔** 📨 💽.
* 🚥 💽 ❌ (✅ 👆 ❌ 🏑), ⚫️ ⛓ 👈 *👆* 📱 📟 💔, 🚫 🛬 ⚫️❔ ⚫️ 🔜, &amp; ⚫️ 🔜 📨 💽 ❌ ↩️ 🛬 ❌ 💽. 👉 🌌 👆 &amp; 👆 👩‍💻 💪 🎯 👈 👫 🔜 📨 💽 &amp; 💽 💠 📈.
* 🚮 **🎻 🔗** 📨, 🗄 *➡ 🛠️*.
* 👉 🔜 ⚙️ **🏧 🩺**.
* ⚫️ 🔜 ⚙️ 🏧 👩‍💻 📟 ⚡ 🧰.
✋️ 🏆 🥈:
* ⚫️ 🔜 **📉 &amp; ⛽** 🔢 📊 ⚫️❔ 🔬 📨 🆎.
* 👉 ✴️ ⚠ **💂‍♂**, 👥 🔜 👀 🌅 👈 🔛.
## `response_model` 🔢
📤 💼 🌐❔ 👆 💪 ⚖️ 💚 📨 💽 👈 🚫 ⚫️❔ ⚫️❔ 🆎 📣.
🖼, 👆 💪 💚 **📨 📖** ⚖️ 💽 🎚, ✋️ **📣 ⚫️ Pydantic 🏷**. 👉 🌌 Pydantic 🏷 🔜 🌐 💽 🧾, 🔬, ♒️. 🎚 👈 👆 📨 (✅ 📖 ⚖️ 💽 🎚).
🚥 👆 🚮 📨 🆎 ✍, 🧰 &amp; 👨‍🎨 🔜 😭 ⏮️ (☑) ❌ 💬 👆 👈 👆 🔢 🛬 🆎 (✅#️⃣) 👈 🎏 ⚪️➡️ ⚫️❔ 👆 📣 (✅ Pydantic 🏷).
📚 💼, 👆 💪 ⚙️ *➡ 🛠️ 👨‍🎨* 🔢 `response_model` ↩️ 📨 🆎.
👆 💪 ⚙️ `response_model` 🔢 🙆 *➡ 🛠️*:
* `@app.get()`
* `@app.post()`
* `@app.put()`
* `@app.delete()`
* ♒️.
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="17 22 24-27"
{!> ../../../docs_src/response_model/tutorial001.py!}
```
=== "🐍 3⃣.9️⃣ &amp; 🔛"
```Python hl_lines="17 22 24-27"
{!> ../../../docs_src/response_model/tutorial001_py39.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="17 22 24-27"
{!> ../../../docs_src/response_model/tutorial001_py310.py!}
```
!!! note
👀 👈 `response_model` 🔢 "👨‍🎨" 👩‍🔬 (`get`, `post`, ♒️). 🚫 👆 *➡ 🛠️ 🔢*, 💖 🌐 🔢 &amp; 💪.
`response_model` 📨 🎏 🆎 👆 🔜 📣 Pydantic 🏷 🏑,, ⚫️ 💪 Pydantic 🏷, ✋️ ⚫️ 💪, ✅ `list` Pydantic 🏷, 💖 `List[Item]`.
FastAPI 🔜 ⚙️ 👉 `response_model` 🌐 💽 🧾, 🔬, ♒️. &amp; **🗜 &amp; ⛽ 🔢 📊** 🚮 🆎 📄.
!!! tip
🚥 👆 ✔️ ⚠ 🆎 ✅ 👆 👨‍🎨, ✍, ♒️, 👆 💪 📣 🔢 📨 🆎 `Any`.
👈 🌌 👆 💬 👨‍🎨 👈 👆 😫 🛬 🕳. ✋️ FastAPI 🔜 💽 🧾, 🔬, 🖥, ♒️. ⏮️ `response_model`.
### `response_model` 📫
🚥 👆 📣 👯‍♂️ 📨 🆎 &amp; `response_model`, `response_model` 🔜 ✊ 📫 &amp; ⚙️ FastAPI.
👉 🌌 👆 💪 🚮 ☑ 🆎 ✍ 👆 🔢 🕐❔ 👆 🛬 🆎 🎏 🌘 📨 🏷, ⚙️ 👨‍🎨 &amp; 🧰 💖 ✍. &amp; 👆 💪 ✔️ FastAPI 💽 🔬, 🧾, ♒️. ⚙️ `response_model`.
👆 💪 ⚙️ `response_model=None` ❎ 🏗 📨 🏷 👈 *➡ 🛠️*, 👆 5⃣📆 💪 ⚫️ 🚥 👆 ❎ 🆎 ✍ 👜 👈 🚫 ☑ Pydantic 🏑, 👆 🔜 👀 🖼 👈 1⃣ 📄 🔛.
## 📨 🎏 🔢 💽
📥 👥 📣 `UserIn` 🏷, ⚫️ 🔜 🔌 🔢 🔐:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="9 11"
{!> ../../../docs_src/response_model/tutorial002.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="7 9"
{!> ../../../docs_src/response_model/tutorial002_py310.py!}
```
!!! info
⚙️ `EmailStr`, 🥇 ❎ <a href="https://github.com/JoshData/python-email-validator" class="external-link" target="_blank">`email_validator`</a>.
🤶 Ⓜ. `pip install email-validator`
⚖️ `pip install pydantic[email]`.
&amp; 👥 ⚙️ 👉 🏷 📣 👆 🔢 &amp; 🎏 🏷 📣 👆 🔢:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="18"
{!> ../../../docs_src/response_model/tutorial002.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="16"
{!> ../../../docs_src/response_model/tutorial002_py310.py!}
```
🔜, 🕐❔ 🖥 🏗 👩‍💻 ⏮️ 🔐, 🛠️ 🔜 📨 🎏 🔐 📨.
👉 💼, ⚫️ 💪 🚫 ⚠, ↩️ ⚫️ 🎏 👩‍💻 📨 🔐.
✋️ 🚥 👥 ⚙️ 🎏 🏷 1⃣ *➡ 🛠️*, 👥 💪 📨 👆 👩‍💻 🔐 🔠 👩‍💻.
!!! danger
🙅 🏪 ✅ 🔐 👩‍💻 ⚖️ 📨 ⚫️ 📨 💖 👉, 🚥 👆 💭 🌐 ⚠ &amp; 👆 💭 ⚫️❔ 👆 🔨.
## 🚮 🔢 🏷
👥 💪 ↩️ ✍ 🔢 🏷 ⏮️ 🔢 🔐 &amp; 🔢 🏷 🍵 ⚫️:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="9 11 16"
{!> ../../../docs_src/response_model/tutorial003.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="9 11 16"
{!> ../../../docs_src/response_model/tutorial003_py310.py!}
```
📥, ✋️ 👆 *➡ 🛠️ 🔢* 🛬 🎏 🔢 👩‍💻 👈 🔌 🔐:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="24"
{!> ../../../docs_src/response_model/tutorial003.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="24"
{!> ../../../docs_src/response_model/tutorial003_py310.py!}
```
...👥 📣 `response_model` 👆 🏷 `UserOut`, 👈 🚫 🔌 🔐:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="22"
{!> ../../../docs_src/response_model/tutorial003.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="22"
{!> ../../../docs_src/response_model/tutorial003_py310.py!}
```
, **FastAPI** 🔜 ✊ 💅 🖥 👅 🌐 💽 👈 🚫 📣 🔢 🏷 (⚙️ Pydantic).
### `response_model` ⚖️ 📨 🆎
👉 💼, ↩️ 2⃣ 🏷 🎏, 🚥 👥 ✍ 🔢 📨 🆎 `UserOut`, 👨‍🎨 &amp; 🧰 🔜 😭 👈 👥 🛬 ❌ 🆎, 📚 🎏 🎓.
👈 ⚫️❔ 👉 🖼 👥 ✔️ 📣 ⚫️ `response_model` 🔢.
...✋️ 😣 👂 🔛 👀 ❔ ❎ 👈.
## 📨 🆎 &amp; 💽 🖥
➡️ 😣 ⚪️➡️ ⏮️ 🖼. 👥 💚 **✍ 🔢 ⏮️ 1⃣ 🆎** ✋️ 📨 🕳 👈 🔌 **🌅 💽**.
👥 💚 FastAPI 🚧 **🖥** 📊 ⚙️ 📨 🏷.
⏮️ 🖼, ↩️ 🎓 🎏, 👥 ✔️ ⚙️ `response_model` 🔢. ✋️ 👈 ⛓ 👈 👥 🚫 🤚 🐕‍🦺 ⚪️➡️ 👨‍🎨 &amp; 🧰 ✅ 🔢 📨 🆎.
✋️ 🌅 💼 🌐❔ 👥 💪 🕳 💖 👉, 👥 💚 🏷 **⛽/❎** 📊 👉 🖼.
&amp; 👈 💼, 👥 💪 ⚙️ 🎓 &amp; 🧬 ✊ 📈 🔢 **🆎 ✍** 🤚 👍 🐕‍🦺 👨‍🎨 &amp; 🧰, &amp; 🤚 FastAPI **💽 🖥**.
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="9-13 15-16 20"
{!> ../../../docs_src/response_model/tutorial003_01.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="7-10 13-14 18"
{!> ../../../docs_src/response_model/tutorial003_01_py310.py!}
```
⏮️ 👉, 👥 🤚 🏭 🐕‍🦺, ⚪️➡️ 👨‍🎨 &amp; ✍ 👉 📟 ☑ ⚖ 🆎, ✋️ 👥 🤚 💽 🖥 ⚪️➡️ FastAPI.
❔ 🔨 👉 👷 ❓ ➡️ ✅ 👈 👅. 👶
### 🆎 ✍ &amp; 🏭
🥇 ➡️ 👀 ❔ 👨‍🎨, ✍ &amp; 🎏 🧰 🔜 👀 👉.
`BaseUser` ✔️ 🧢 🏑. ⤴️ `UserIn` 😖 ⚪️➡️ `BaseUser` &amp; 🚮 `password` 🏑,, ⚫️ 🔜 🔌 🌐 🏑 ⚪️➡️ 👯‍♂️ 🏷.
👥 ✍ 🔢 📨 🆎 `BaseUser`, ✋️ 👥 🤙 🛬 `UserIn` 👐.
👨‍🎨, ✍, &amp; 🎏 🧰 🏆 🚫 😭 🔃 👉 ↩️, ⌨ ⚖, `UserIn` 🏿 `BaseUser`, ❔ ⛓ ⚫️ *☑* 🆎 🕐❔ ⚫️❔ ⌛ 🕳 👈 `BaseUser`.
### FastAPI 💽 🖥
🔜, FastAPI, ⚫️ 🔜 👀 📨 🆎 &amp; ⚒ 💭 👈 ⚫️❔ 👆 📨 🔌 **🕴** 🏑 👈 📣 🆎.
FastAPI 🔨 📚 👜 🔘 ⏮️ Pydantic ⚒ 💭 👈 📚 🎏 🚫 🎓 🧬 🚫 ⚙️ 📨 💽 🖥, ⏪ 👆 💪 🔚 🆙 🛬 🌅 🌅 💽 🌘 ⚫️❔ 👆 📈.
👉 🌌, 👆 💪 🤚 🏆 👯‍♂️ 🌏: 🆎 ✍ ⏮️ **🏭 🐕‍🦺** &amp; **💽 🖥**.
## 👀 ⚫️ 🩺
🕐❔ 👆 👀 🏧 🩺, 👆 💪 ✅ 👈 🔢 🏷 &amp; 🔢 🏷 🔜 👯‍♂️ ✔️ 👫 👍 🎻 🔗:
<img src="/img/tutorial/response-model/image01.png">
&amp; 👯‍♂️ 🏷 🔜 ⚙️ 🎓 🛠️ 🧾:
<img src="/img/tutorial/response-model/image02.png">
## 🎏 📨 🆎 ✍
📤 5⃣📆 💼 🌐❔ 👆 📨 🕳 👈 🚫 ☑ Pydantic 🏑 &amp; 👆 ✍ ⚫️ 🔢, 🕴 🤚 🐕‍🦺 🚚 🏭 (👨‍🎨, ✍, ♒️).
### 📨 📨 🔗
🏆 ⚠ 💼 🔜 [🛬 📨 🔗 🔬 ⏪ 🏧 🩺](../advanced/response-directly.md){.internal-link target=_blank}.
```Python hl_lines="8 10-11"
{!> ../../../docs_src/response_model/tutorial003_02.py!}
```
👉 🙅 💼 🍵 🔁 FastAPI ↩️ 📨 🆎 ✍ 🎓 (⚖️ 🏿) `Response`.
&amp; 🧰 🔜 😄 ↩️ 👯‍♂️ `RedirectResponse` &amp; `JSONResponse` 🏿 `Response`, 🆎 ✍ ☑.
### ✍ 📨 🏿
👆 💪 ⚙️ 🏿 `Response` 🆎 ✍:
```Python hl_lines="8-9"
{!> ../../../docs_src/response_model/tutorial003_03.py!}
```
👉 🔜 👷 ↩️ `RedirectResponse` 🏿 `Response`, &amp; FastAPI 🔜 🔁 🍵 👉 🙅 💼.
### ❌ 📨 🆎 ✍
✋️ 🕐❔ 👆 📨 🎏 ❌ 🎚 👈 🚫 ☑ Pydantic 🆎 (✅ 💽 🎚) &amp; 👆 ✍ ⚫️ 💖 👈 🔢, FastAPI 🔜 🔄 ✍ Pydantic 📨 🏷 ⚪️➡️ 👈 🆎 ✍, &amp; 🔜 ❌.
🎏 🔜 🔨 🚥 👆 ✔️ 🕳 💖 <abbr title='A union between multiple types means "any of these types".'>🇪🇺</abbr> 🖖 🎏 🆎 🌐❔ 1⃣ ⚖️ 🌅 👫 🚫 ☑ Pydantic 🆎, 🖼 👉 🔜 ❌ 👶:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="10"
{!> ../../../docs_src/response_model/tutorial003_04.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="8"
{!> ../../../docs_src/response_model/tutorial003_04_py310.py!}
```
...👉 ❌ ↩️ 🆎 ✍ 🚫 Pydantic 🆎 &amp; 🚫 👁 `Response` 🎓 ⚖️ 🏿, ⚫️ 🇪🇺 (🙆 2⃣) 🖖 `Response` &amp; `dict`.
### ❎ 📨 🏷
▶️ ⚪️➡️ 🖼 🔛, 👆 5⃣📆 🚫 💚 ✔️ 🔢 💽 🔬, 🧾, 🖥, ♒️. 👈 🎭 FastAPI.
✋️ 👆 💪 💚 🚧 📨 🆎 ✍ 🔢 🤚 🐕‍🦺 ⚪️➡️ 🧰 💖 👨‍🎨 &amp; 🆎 ☑ (✅ ✍).
👉 💼, 👆 💪 ❎ 📨 🏷 ⚡ ⚒ `response_model=None`:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="9"
{!> ../../../docs_src/response_model/tutorial003_05.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="7"
{!> ../../../docs_src/response_model/tutorial003_05_py310.py!}
```
👉 🔜 ⚒ FastAPI 🚶 📨 🏷 ⚡ &amp; 👈 🌌 👆 💪 ✔️ 🙆 📨 🆎 ✍ 👆 💪 🍵 ⚫️ 🤕 👆 FastAPI 🈸. 👶
## 📨 🏷 🔢 🔢
👆 📨 🏷 💪 ✔️ 🔢 💲, 💖:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="11 13-14"
{!> ../../../docs_src/response_model/tutorial004.py!}
```
=== "🐍 3⃣.9️⃣ &amp; 🔛"
```Python hl_lines="11 13-14"
{!> ../../../docs_src/response_model/tutorial004_py39.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```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️⃣ &amp; 🔛"
```Python hl_lines="24"
{!> ../../../docs_src/response_model/tutorial004.py!}
```
=== "🐍 3⃣.9️⃣ &amp; 🔛"
```Python hl_lines="24"
{!> ../../../docs_src/response_model/tutorial004_py39.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="22"
{!> ../../../docs_src/response_model/tutorial004_py310.py!}
```
&amp; 👈 🔢 💲 🏆 🚫 🔌 📨, 🕴 💲 🤙 ⚒.
, 🚥 👆 📨 📨 👈 *➡ 🛠️* 🏬 ⏮️ 🆔 `foo`, 📨 (🚫 ✅ 🔢 💲) 🔜:
```JSON
{
"name": "Foo",
"price": 50.2
}
```
!!! info
FastAPI ⚙️ Pydantic 🏷 `.dict()` ⏮️ <a href="https://pydantic-docs.helpmanual.io/usage/exporting_models/#modeldict" class="external-link" target="_blank">🚮 `exclude_unset` 🔢</a> 🏆 👉.
!!! info
👆 💪 ⚙️:
* `response_model_exclude_defaults=True`
* `response_model_exclude_none=True`
🔬 <a href="https://pydantic-docs.helpmanual.io/usage/exporting_models/#modeldict" class="external-link" target="_blank">Pydantic 🩺</a> `exclude_defaults` &amp; `exclude_none`.
#### 📊 ⏮️ 💲 🏑 ⏮️ 🔢
✋️ 🚥 👆 📊 ✔️ 💲 🏷 🏑 ⏮️ 🔢 💲, 💖 🏬 ⏮️ 🆔 `bar`:
```Python hl_lines="3 5"
{
"name": "Bar",
"description": "The bartenders",
"price": 62,
"tax": 20.2
}
```
👫 🔜 🔌 📨.
#### 📊 ⏮️ 🎏 💲 🔢
🚥 📊 ✔️ 🎏 💲 🔢 🕐, 💖 🏬 ⏮️ 🆔 `baz`:
```Python hl_lines="3 5-6"
{
"name": "Baz",
"description": None,
"price": 50.2,
"tax": 10.5,
"tags": []
}
```
FastAPI 🙃 🥃 (🤙, Pydantic 🙃 🥃) 🤔 👈, ✋️ `description`, `tax`, &amp; `tags` ✔️ 🎏 💲 🔢, 👫 ⚒ 🎯 (↩️ ✊ ⚪️➡️ 🔢).
, 👫 🔜 🔌 🎻 📨.
!!! tip
👀 👈 🔢 💲 💪 🕳, 🚫 🕴 `None`.
👫 💪 📇 (`[]`), `float` `10.5`, ♒️.
### `response_model_include` &amp; `response_model_exclude`
👆 💪 ⚙️ *➡ 🛠️ 👨‍🎨* 🔢 `response_model_include` &amp; `response_model_exclude`.
👫 ✊ `set` `str` ⏮️ 📛 🔢 🔌 (❎ 🎂) ⚖️ 🚫 (✅ 🎂).
👉 💪 ⚙️ ⏩ ⌨ 🚥 👆 ✔️ 🕴 1⃣ Pydantic 🏷 &amp; 💚 ❎ 💽 ⚪️➡️ 🔢.
!!! tip
✋️ ⚫️ 👍 ⚙️ 💭 🔛, ⚙️ 💗 🎓, ↩️ 👫 🔢.
👉 ↩️ 🎻 🔗 🏗 👆 📱 🗄 (&amp; 🩺) 🔜 1⃣ 🏁 🏷, 🚥 👆 ⚙️ `response_model_include` ⚖️ `response_model_exclude` 🚫 🔢.
👉 ✔ `response_model_by_alias` 👈 👷 ➡.
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="31 37"
{!> ../../../docs_src/response_model/tutorial005.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="29 35"
{!> ../../../docs_src/response_model/tutorial005_py310.py!}
```
!!! tip
❕ `{"name", "description"}` ✍ `set` ⏮️ 📚 2⃣ 💲.
⚫️ 🌓 `set(["name", "description"])`.
#### ⚙️ `list`Ⓜ ↩️ `set`Ⓜ
🚥 👆 💭 ⚙️ `set` &amp; ⚙️ `list` ⚖️ `tuple` ↩️, FastAPI 🔜 🗜 ⚫️ `set` &amp; ⚫️ 🔜 👷 ☑:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="31 37"
{!> ../../../docs_src/response_model/tutorial006.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="29 35"
{!> ../../../docs_src/response_model/tutorial006_py310.py!}
```
## 🌃
⚙️ *➡ 🛠️ 👨‍🎨* 🔢 `response_model` 🔬 📨 🏷 &amp; ✴️ 🚚 📢 💽 ⛽ 👅.
⚙️ `response_model_exclude_unset` 📨 🕴 💲 🎯 ⚒.

View File

@@ -0,0 +1,89 @@
# 📨 👔 📟
🎏 🌌 👆 💪 ✔ 📨 🏷, 👆 💪 📣 🇺🇸🔍 👔 📟 ⚙️ 📨 ⏮️ 🔢 `status_code` 🙆 *➡ 🛠️*:
* `@app.get()`
* `@app.post()`
* `@app.put()`
* `@app.delete()`
* ♒️.
```Python hl_lines="6"
{!../../../docs_src/response_status_code/tutorial001.py!}
```
!!! note
👀 👈 `status_code` 🔢 "👨‍🎨" 👩‍🔬 (`get`, `post`, ♒️). 🚫 👆 *➡ 🛠️ 🔢*, 💖 🌐 🔢 &amp; 💪.
`status_code` 🔢 📨 🔢 ⏮️ 🇺🇸🔍 👔 📟.
!!! info
`status_code` 💪 👐 📨 `IntEnum`, ✅ 🐍 <a href="https://docs.python.org/3/library/http.html#http.HTTPStatus" class="external-link" target="_blank">`http.HTTPStatus`</a>.
⚫️ 🔜:
* 📨 👈 👔 📟 📨.
* 📄 ⚫️ ✅ 🗄 🔗 ( &amp; , 👩‍💻 🔢):
<img src="/img/tutorial/response-status-code/image01.png">
!!! note
📨 📟 (👀 ⏭ 📄) 🎦 👈 📨 🔨 🚫 ✔️ 💪.
FastAPI 💭 👉, &amp; 🔜 🏭 🗄 🩺 👈 🇵🇸 📤 🙅‍♂ 📨 💪.
## 🔃 🇺🇸🔍 👔 📟
!!! note
🚥 👆 ⏪ 💭 ⚫️❔ 🇺🇸🔍 👔 📟, 🚶 ⏭ 📄.
🇺🇸🔍, 👆 📨 🔢 👔 📟 3⃣ 9⃣ 🍕 📨.
👫 👔 📟 ✔️ 📛 🔗 🤔 👫, ✋️ ⚠ 🍕 🔢.
📏:
* `100` &amp; 🔛 "". 👆 🛎 ⚙️ 👫 🔗. 📨 ⏮️ 👫 👔 📟 🚫🔜 ✔️ 💪.
* **`200`** &amp; 🔛 "🏆" 📨. 👫 🕐 👆 🔜 ⚙️ 🏆.
* `200` 🔢 👔 📟, ❔ ⛓ 🌐 "👌".
* 1⃣ 🖼 🔜 `201`, "✍". ⚫️ 🛎 ⚙️ ⏮️ 🏗 🆕 ⏺ 💽.
* 🎁 💼 `204`, "🙅‍♂ 🎚". 👉 📨 ⚙️ 🕐❔ 📤 🙅‍♂ 🎚 📨 👩‍💻, &amp; 📨 🔜 🚫 ✔️ 💪.
* **`300`** &amp; 🔛 "❎". 📨 ⏮️ 👫 👔 📟 5⃣📆 ⚖️ 5⃣📆 🚫 ✔️ 💪, 🌖 `304`, "🚫 🔀", ❔ 🔜 🚫 ✔️ 1⃣.
* **`400`** &amp; 🔛 "👩‍💻 ❌" 📨. 👫 🥈 🆎 👆 🔜 🎲 ⚙️ 🏆.
* 🖼 `404`, "🚫 🔎" 📨.
* 💊 ❌ ⚪️➡️ 👩‍💻, 👆 💪 ⚙️ `400`.
* `500` &amp; 🔛 💽 ❌. 👆 🌖 🙅 ⚙️ 👫 🔗. 🕐❔ 🕳 🚶 ❌ 🍕 👆 🈸 📟, ⚖️ 💽, ⚫️ 🔜 🔁 📨 1⃣ 👫 👔 📟.
!!! tip
💭 🌅 🔃 🔠 👔 📟 &amp; ❔ 📟 ⚫️❔, ✅ <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Status" class="external-link" target="_blank"><abbr title="Mozilla Developer Network">🏇</abbr> 🧾 🔃 🇺🇸🔍 👔 📟</a>.
## ⌨ 💭 📛
➡️ 👀 ⏮️ 🖼 🔄:
```Python hl_lines="6"
{!../../../docs_src/response_status_code/tutorial001.py!}
```
`201` 👔 📟 "✍".
✋️ 👆 🚫 ✔️ ✍ ⚫️❔ 🔠 👉 📟 ⛓.
👆 💪 ⚙️ 🏪 🔢 ⚪️➡️ `fastapi.status`.
```Python hl_lines="1 6"
{!../../../docs_src/response_status_code/tutorial002.py!}
```
👫 🏪, 👫 🧑‍🤝‍🧑 🎏 🔢, ✋️ 👈 🌌 👆 💪 ⚙️ 👨‍🎨 📋 🔎 👫:
<img src="/img/tutorial/response-status-code/image02.png">
!!! note "📡 "
👆 💪 ⚙️ `from starlette import status`.
**FastAPI** 🚚 🎏 `starlette.status` `fastapi.status` 🏪 👆, 👩‍💻. ✋️ ⚫️ 👟 🔗 ⚪️➡️ 💃.
## 🔀 🔢
⏪, [🏧 👩‍💻 🦮](../advanced/response-change-status-code.md){.internal-link target=_blank}, 👆 🔜 👀 ❔ 📨 🎏 👔 📟 🌘 🔢 👆 📣 📥.

View File

@@ -0,0 +1,141 @@
# 📣 📨 🖼 💽
👆 💪 📣 🖼 💽 👆 📱 💪 📨.
📥 📚 🌌 ⚫️.
## Pydantic `schema_extra`
👆 💪 📣 `example` Pydantic 🏷 ⚙️ `Config` &amp; `schema_extra`, 🔬 <a href="https://pydantic-docs.helpmanual.io/usage/schema/#schema-customization" class="external-link" target="_blank">Pydantic 🩺: 🔗 🛃</a>:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="15-23"
{!> ../../../docs_src/schema_extra_example/tutorial001.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="13-21"
{!> ../../../docs_src/schema_extra_example/tutorial001_py310.py!}
```
👈 🔜 🚮-🔢 **🎻 🔗** 👈 🏷, &amp; ⚫️ 🔜 ⚙️ 🛠️ 🩺.
!!! tip
👆 💪 ⚙️ 🎏 ⚒ ↔ 🎻 🔗 &amp; 🚮 👆 👍 🛃 .
🖼 👆 💪 ⚙️ ⚫️ 🚮 🗃 🕸 👩‍💻 🔢, ♒️.
## `Field` 🌖 ❌
🕐❔ ⚙️ `Field()` ⏮️ Pydantic 🏷, 👆 💪 📣 **🎻 🔗** 🚶‍♀️ 🙆 🎏 ❌ ❌ 🔢.
👆 💪 ⚙️ 👉 🚮 `example` 🔠 🏑:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="4 10-13"
{!> ../../../docs_src/schema_extra_example/tutorial002.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="2 8-11"
{!> ../../../docs_src/schema_extra_example/tutorial002_py310.py!}
```
!!! warning
🚧 🤯 👈 📚 ❌ 🚶‍♀️ 🏆 🚫 🚮 🙆 🔬, 🕴 , 🧾 🎯.
## `example` &amp; `examples` 🗄
🕐❔ ⚙️ 🙆:
* `Path()`
* `Query()`
* `Header()`
* `Cookie()`
* `Body()`
* `Form()`
* `File()`
👆 💪 📣 💽 `example` ⚖️ 👪 `examples` ⏮️ 🌖 👈 🔜 🚮 **🗄**.
### `Body` ⏮️ `example`
📥 👥 🚶‍♀️ `example` 📊 ⌛ `Body()`:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="20-25"
{!> ../../../docs_src/schema_extra_example/tutorial003.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="18-23"
{!> ../../../docs_src/schema_extra_example/tutorial003_py310.py!}
```
### 🖼 🩺 🎚
⏮️ 🙆 👩‍🔬 🔛 ⚫️ 🔜 👀 💖 👉 `/docs`:
<img src="/img/tutorial/body-fields/image01.png">
### `Body` ⏮️ 💗 `examples`
👐 👁 `example`, 👆 💪 🚶‍♀️ `examples` ⚙️ `dict` ⏮️ **💗 🖼**, 🔠 ⏮️ 👈 🔜 🚮 **🗄** 💁‍♂️.
🔑 `dict` 🔬 🔠 🖼, &amp; 🔠 💲 1⃣ `dict`.
🔠 🎯 🖼 `dict` `examples` 💪 🔌:
* `summary`: 📏 📛 🖼.
* `description`: 📏 📛 👈 💪 🔌 ✍ ✍.
* `value`: 👉 ☑ 🖼 🎦, ✅ `dict`.
* `externalValue`: 🎛 `value`, 📛 ☝ 🖼. 👐 👉 5⃣📆 🚫 🐕‍🦺 📚 🧰 `value`.
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="21-47"
{!> ../../../docs_src/schema_extra_example/tutorial004.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="19-45"
{!> ../../../docs_src/schema_extra_example/tutorial004_py310.py!}
```
### 🖼 🩺 🎚
⏮️ `examples` 🚮 `Body()` `/docs` 🔜 👀 💖:
<img src="/img/tutorial/body-fields/image02.png">
## 📡
!!! warning
👉 📶 📡 🔃 🐩 **🎻 🔗** &amp; **🗄**.
🚥 💭 🔛 ⏪ 👷 👆, 👈 💪 🥃, &amp; 👆 🎲 🚫 💪 👉 , 💭 🆓 🚶 👫.
🕐❔ 👆 🚮 🖼 🔘 Pydantic 🏷, ⚙️ `schema_extra` ⚖️ `Field(example="something")` 👈 🖼 🚮 **🎻 🔗** 👈 Pydantic 🏷.
&amp; 👈 **🎻 🔗** Pydantic 🏷 🔌 **🗄** 👆 🛠️, &amp; ⤴️ ⚫️ ⚙️ 🩺 🎚.
**🎻 🔗** 🚫 🤙 ✔️ 🏑 `example` 🐩. ⏮️ ⏬ 🎻 🔗 🔬 🏑 <a href="https://json-schema.org/draft/2019-09/json-schema-validation.html#rfc.section.9.5" class="external-link" target="_blank">`examples`</a>, ✋️ 🗄 3⃣.0️⃣.3️⃣ ⚓️ 🔛 🗝 ⏬ 🎻 🔗 👈 🚫 ✔️ `examples`.
, 🗄 3⃣.0️⃣.3️⃣ 🔬 🚮 👍 <a href="https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.3.md#fixed-fields-20" class="external-link" target="_blank">`example`</a> 🔀 ⏬ **🎻 🔗** ⚫️ ⚙️, 🎏 🎯 (✋️ ⚫️ 👁 `example`, 🚫 `examples`), &amp; 👈 ⚫️❔ ⚙️ 🛠️ 🩺 🎚 (⚙️ 🦁 🎚).
, 👐 `example` 🚫 🍕 🎻 🔗, ⚫️ 🍕 🗄 🛃 ⏬ 🎻 🔗, &amp; 👈 ⚫️❔ 🔜 ⚙️ 🩺 🎚.
✋️ 🕐❔ 👆 ⚙️ `example` ⚖️ `examples` ⏮️ 🙆 🎏 🚙 (`Query()`, `Body()`, ♒️.) 📚 🖼 🚫 🚮 🎻 🔗 👈 🔬 👈 💽 (🚫 🗄 👍 ⏬ 🎻 🔗), 👫 🚮 🔗 *➡ 🛠️* 📄 🗄 (🏞 🍕 🗄 👈 ⚙️ 🎻 🔗).
`Path()`, `Query()`, `Header()`, &amp; `Cookie()`, `example` ⚖️ `examples` 🚮 <a href="https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#parameter-object" class="external-link" target="_blank">🗄 🔑, `Parameter Object` (🔧)</a>.
&amp; `Body()`, `File()`, &amp; `Form()`, `example` ⚖️ `examples` 📊 🚮 <a href="https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#mediaTypeObject" class="external-link" target="_blank">🗄 🔑, `Request Body Object`, 🏑 `content`, 🔛 `Media Type Object` (🔧)</a>.
🔛 🎏 ✋, 📤 🆕 ⏬ 🗄: **3⃣.1️⃣.0️⃣**, ⏳ 🚀. ⚫️ ⚓️ 🔛 ⏪ 🎻 🔗 &amp; 🏆 🛠️ ⚪️➡️ 🗄 🛃 ⏬ 🎻 🔗 ❎, 💱 ⚒ ⚪️➡️ ⏮️ ⏬ 🎻 🔗, 🌐 👫 🤪 🔺 📉. 👐, 🦁 🎚 ⏳ 🚫 🐕‍🦺 🗄 3⃣.1️⃣.0️⃣,, 🔜, ⚫️ 👍 😣 ⚙️ 💭 🔛.

View File

@@ -0,0 +1,182 @@
# 💂‍♂ - 🥇 🔁
➡️ 🌈 👈 👆 ✔️ 👆 **👩‍💻** 🛠️ 🆔.
&amp; 👆 ✔️ **🕸** 1⃣ 🆔 ⚖️ 🎏 ➡ 🎏 🆔 (⚖️ 📱 🈸).
&amp; 👆 💚 ✔️ 🌌 🕸 🔓 ⏮️ 👩‍💻, ⚙️ **🆔** &amp; **🔐**.
👥 💪 ⚙️ **Oauth2** 🏗 👈 ⏮️ **FastAPI**.
✋️ ➡️ 🖊 👆 🕰 👂 🌕 📏 🔧 🔎 👈 🐥 🍖 👆 💪.
➡️ ⚙️ 🧰 🚚 **FastAPI** 🍵 💂‍♂.
## ❔ ⚫️ 👀
➡️ 🥇 ⚙️ 📟 &amp; 👀 ❔ ⚫️ 👷, &amp; ⤴️ 👥 🔜 👟 🔙 🤔 ⚫️❔ 😥.
## ✍ `main.py`
📁 🖼 📁 `main.py`:
```Python
{!../../../docs_src/security/tutorial001.py!}
```
## 🏃 ⚫️
!!! info
🥇 ❎ <a href="https://andrew-d.github.io/python-multipart/" class="external-link" target="_blank">`python-multipart`</a>.
🤶 Ⓜ. `pip install python-multipart`.
👉 ↩️ **Oauth2** ⚙️ "📨 📊" 📨 `username` &amp; `password`.
🏃 🖼 ⏮️:
<div class="termy">
```console
$ uvicorn main:app --reload
<span style="color: green;">INFO</span>: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
```
</div>
## ✅ ⚫️
🚶 🎓 🩺: <a href="http://127.0.0.1:8000/docs" class="external-link" target="_blank">http://127.0.0.1:8000/docs</a>.
👆 🔜 👀 🕳 💖 👉:
<img src="/img/tutorial/security/image01.png">
!!! check "✔ 🔼 ❗"
👆 ⏪ ✔️ ✨ 🆕 "✔" 🔼.
&amp; 👆 *➡ 🛠️* ✔️ 🐥 🔒 🔝-▶️️ ↩ 👈 👆 💪 🖊.
&amp; 🚥 👆 🖊 ⚫️, 👆 ✔️ 🐥 ✔ 📨 🆎 `username` &amp; `password` (&amp; 🎏 📦 🏑):
<img src="/img/tutorial/security/image02.png">
!!! note
⚫️ 🚫 🤔 ⚫️❔ 👆 🆎 📨, ⚫️ 🏆 🚫 👷. ✋️ 👥 🔜 🤚 📤.
👉 ↗️ 🚫 🕸 🏁 👩‍💻, ✋️ ⚫️ 👑 🏧 🧰 📄 🖥 🌐 👆 🛠️.
⚫️ 💪 ⚙️ 🕸 🏉 (👈 💪 👆).
⚫️ 💪 ⚙️ 🥉 🥳 🈸 &amp; ⚙️.
&amp; ⚫️ 💪 ⚙️ 👆, , ✅ &amp; 💯 🎏 🈸.
## `password` 💧
🔜 ➡️ 🚶 🔙 👄 &amp; 🤔 ⚫️❔ 🌐 👈.
`password` "💧" 1⃣ 🌌 ("💧") 🔬 Oauth2⃣, 🍵 💂‍♂ &amp; 🤝.
Oauth2⃣ 🔧 👈 👩‍💻 ⚖️ 🛠️ 💪 🔬 💽 👈 🔓 👩‍💻.
✋️ 👉 💼, 🎏 **FastAPI** 🈸 🔜 🍵 🛠️ &amp; 🤝.
, ➡️ 📄 ⚫️ ⚪️➡️ 👈 📉 ☝ 🎑:
* 👩‍💻 🆎 `username` &amp; `password` 🕸, &amp; 🎯 `Enter`.
* 🕸 (🏃‍♂ 👩‍💻 🖥) 📨 👈 `username` &amp; `password` 🎯 📛 👆 🛠️ (📣 ⏮️ `tokenUrl="token"`).
* 🛠️ ✅ 👈 `username` &amp; `password`, &amp; 📨 ⏮️ "🤝" (👥 🚫 🛠️ 🙆 👉).
* "🤝" 🎻 ⏮️ 🎚 👈 👥 💪 ⚙️ ⏪ ✔ 👉 👩‍💻.
* 🛎, 🤝 ⚒ 🕛 ⏮️ 🕰.
* , 👩‍💻 🔜 ✔️ 🕹 🔄 ☝ ⏪.
* &amp; 🚥 🤝 📎, ⚠ 🌘. ⚫️ 🚫 💖 🧲 🔑 👈 🔜 👷 ♾ (🏆 💼).
* 🕸 🏪 👈 🤝 🍕 👱.
* 👩‍💻 🖊 🕸 🚶 1⃣ 📄 🕸 🕸 📱.
* 🕸 💪 ☕ 🌅 💽 ⚪️➡️ 🛠️.
* ✋️ ⚫️ 💪 🤝 👈 🎯 🔗.
* , 🔓 ⏮️ 👆 🛠️, ⚫️ 📨 🎚 `Authorization` ⏮️ 💲 `Bearer ` 🤝.
* 🚥 🤝 🔌 `foobar`, 🎚 `Authorization` 🎚 🔜: `Bearer foobar`.
## **FastAPI**'Ⓜ `OAuth2PasswordBearer`
**FastAPI** 🚚 📚 🧰, 🎏 🎚 ⚛, 🛠️ 👫 💂‍♂ ⚒.
👉 🖼 👥 🔜 ⚙️ **Oauth2**, ⏮️ **🔐** 💧, ⚙️ **📨** 🤝. 👥 👈 ⚙️ `OAuth2PasswordBearer` 🎓.
!!! info
"📨" 🤝 🚫 🕴 🎛.
✋️ ⚫️ 🏆 1⃣ 👆 ⚙️ 💼.
&amp; ⚫️ 💪 🏆 🏆 ⚙️ 💼, 🚥 👆 Oauth2⃣ 🕴 &amp; 💭 ⚫️❔ ⚫️❔ 📤 1⃣ 🎛 👈 ♣ 👻 👆 💪.
👈 💼, **FastAPI** 🚚 👆 ⏮️ 🧰 🏗 ⚫️.
🕐❔ 👥 ✍ 👐 `OAuth2PasswordBearer` 🎓 👥 🚶‍♀️ `tokenUrl` 🔢. 👉 🔢 🔌 📛 👈 👩‍💻 (🕸 🏃 👩‍💻 🖥) 🔜 ⚙️ 📨 `username` &amp; `password` ✔ 🤚 🤝.
```Python hl_lines="6"
{!../../../docs_src/security/tutorial001.py!}
```
!!! tip
📥 `tokenUrl="token"` 🔗 ⚖ 📛 `token` 👈 👥 🚫 ✍. ⚫️ ⚖ 📛, ⚫️ 🌓 `./token`.
↩️ 👥 ⚙️ ⚖ 📛, 🚥 👆 🛠️ 🔎 `https://example.com/`, ⤴️ ⚫️ 🔜 🔗 `https://example.com/token`. ✋️ 🚥 👆 🛠️ 🔎 `https://example.com/api/v1/`, ⤴️ ⚫️ 🔜 🔗 `https://example.com/api/v1/token`.
⚙️ ⚖ 📛 ⚠ ⚒ 💭 👆 🈸 🚧 👷 🏧 ⚙️ 💼 💖 [⛅ 🗳](../../advanced/behind-a-proxy.md){.internal-link target=_blank}.
👉 🔢 🚫 ✍ 👈 🔗 / *➡ 🛠️*, ✋️ 📣 👈 📛 `/token` 🔜 1⃣ 👈 👩‍💻 🔜 ⚙️ 🤚 🤝. 👈 ⚙️ 🗄, &amp; ⤴️ 🎓 🛠️ 🧾 ⚙️.
👥 🔜 🔜 ✍ ☑ ➡ 🛠️.
!!! info
🚥 👆 📶 ⚠ "✍" 👆 💪 👎 👗 🔢 📛 `tokenUrl` ↩️ `token_url`.
👈 ↩️ ⚫️ ⚙️ 🎏 📛 🗄 🔌. 👈 🚥 👆 💪 🔬 🌅 🔃 🙆 👫 💂‍♂ ⚖ 👆 💪 📁 &amp; 📋 ⚫️ 🔎 🌖 🔃 ⚫️.
`oauth2_scheme` 🔢 👐 `OAuth2PasswordBearer`, ✋️ ⚫️ "🇧🇲".
⚫️ 💪 🤙:
```Python
oauth2_scheme(some, parameters)
```
, ⚫️ 💪 ⚙️ ⏮️ `Depends`.
### ⚙️ ⚫️
🔜 👆 💪 🚶‍♀️ 👈 `oauth2_scheme` 🔗 ⏮️ `Depends`.
```Python hl_lines="10"
{!../../../docs_src/security/tutorial001.py!}
```
👉 🔗 🔜 🚚 `str` 👈 🛠️ 🔢 `token` *➡ 🛠️ 🔢*.
**FastAPI** 🔜 💭 👈 ⚫️ 💪 ⚙️ 👉 🔗 🔬 "💂‍♂ ⚖" 🗄 🔗 (&amp; 🏧 🛠️ 🩺).
!!! info "📡 "
**FastAPI** 🔜 💭 👈 ⚫️ 💪 ⚙️ 🎓 `OAuth2PasswordBearer` (📣 🔗) 🔬 💂‍♂ ⚖ 🗄 ↩️ ⚫️ 😖 ⚪️➡️ `fastapi.security.oauth2.OAuth2`, ❔ 🔄 😖 ⚪️➡️ `fastapi.security.base.SecurityBase`.
🌐 💂‍♂ 🚙 👈 🛠️ ⏮️ 🗄 (&amp; 🏧 🛠️ 🩺) 😖 ⚪️➡️ `SecurityBase`, 👈 ❔ **FastAPI** 💪 💭 ❔ 🛠️ 👫 🗄.
## ⚫️❔ ⚫️ 🔨
⚫️ 🔜 🚶 &amp; 👀 📨 👈 `Authorization` 🎚, ✅ 🚥 💲 `Bearer ` 🤝, &amp; 🔜 📨 🤝 `str`.
🚥 ⚫️ 🚫 👀 `Authorization` 🎚, ⚖️ 💲 🚫 ✔️ `Bearer ` 🤝, ⚫️ 🔜 📨 ⏮️ 4⃣0⃣1⃣ 👔 📟 ❌ (`UNAUTHORIZED`) 🔗.
👆 🚫 ✔️ ✅ 🚥 🤝 🔀 📨 ❌. 👆 💪 💭 👈 🚥 👆 🔢 🛠️, ⚫️ 🔜 ✔️ `str` 👈 🤝.
👆 💪 🔄 ⚫️ ⏪ 🎓 🩺:
<img src="/img/tutorial/security/image03.png">
👥 🚫 ✔ 🔬 🤝, ✋️ 👈 ▶️ ⏪.
## 🌃
, 3⃣ ⚖️ 4 ⏸, 👆 ⏪ ✔️ 🐒 📨 💂‍♂.

View File

@@ -0,0 +1,151 @@
# 🤚 ⏮️ 👩‍💻
⏮️ 📃 💂‍♂ ⚙️ (❔ 🧢 🔛 🔗 💉 ⚙️) 🤝 *➡ 🛠️ 🔢* `token` `str`:
```Python hl_lines="10"
{!../../../docs_src/security/tutorial001.py!}
```
✋️ 👈 🚫 👈 ⚠.
➡️ ⚒ ⚫️ 🤝 👥 ⏮️ 👩‍💻.
## ✍ 👩‍💻 🏷
🥇, ➡️ ✍ Pydantic 👩‍💻 🏷.
🎏 🌌 👥 ⚙️ Pydantic 📣 💪, 👥 💪 ⚙️ ⚫️ 🙆 🙆:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="5 12-16"
{!> ../../../docs_src/security/tutorial002.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="3 10-14"
{!> ../../../docs_src/security/tutorial002_py310.py!}
```
## ✍ `get_current_user` 🔗
➡️ ✍ 🔗 `get_current_user`.
💭 👈 🔗 💪 ✔️ 🎧-🔗 ❓
`get_current_user` 🔜 ✔️ 🔗 ⏮️ 🎏 `oauth2_scheme` 👥 ✍ ⏭.
🎏 👥 🔨 ⏭ *➡ 🛠️* 🔗, 👆 🆕 🔗 `get_current_user` 🔜 📨 `token` `str` ⚪️➡️ 🎧-🔗 `oauth2_scheme`:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="25"
{!> ../../../docs_src/security/tutorial002.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="23"
{!> ../../../docs_src/security/tutorial002_py310.py!}
```
## 🤚 👩‍💻
`get_current_user` 🔜 ⚙️ (❌) 🚙 🔢 👥 ✍, 👈 ✊ 🤝 `str` &amp; 📨 👆 Pydantic `User` 🏷:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="19-22 26-27"
{!> ../../../docs_src/security/tutorial002.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="17-20 24-25"
{!> ../../../docs_src/security/tutorial002_py310.py!}
```
## 💉 ⏮️ 👩‍💻
🔜 👥 💪 ⚙️ 🎏 `Depends` ⏮️ 👆 `get_current_user` *➡ 🛠️*:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="31"
{!> ../../../docs_src/security/tutorial002.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="29"
{!> ../../../docs_src/security/tutorial002_py310.py!}
```
👀 👈 👥 📣 🆎 `current_user` Pydantic 🏷 `User`.
👉 🔜 🇺🇲 🔘 🔢 ⏮️ 🌐 🛠️ &amp; 🆎 ✅.
!!! tip
👆 5⃣📆 💭 👈 📨 💪 📣 ⏮️ Pydantic 🏷.
📥 **FastAPI** 🏆 🚫 🤚 😨 ↩️ 👆 ⚙️ `Depends`.
!!! check
🌌 👉 🔗 ⚙️ 🏗 ✔ 👥 ✔️ 🎏 🔗 (🎏 "☑") 👈 🌐 📨 `User` 🏷.
👥 🚫 🚫 ✔️ 🕴 1⃣ 🔗 👈 💪 📨 👈 🆎 💽.
## 🎏 🏷
👆 💪 🔜 🤚 ⏮️ 👩‍💻 🔗 *➡ 🛠️ 🔢* &amp; 🙅 ⏮️ 💂‍♂ 🛠️ **🔗 💉** 🎚, ⚙️ `Depends`.
&amp; 👆 💪 ⚙️ 🙆 🏷 ⚖️ 💽 💂‍♂ 📄 (👉 💼, Pydantic 🏷 `User`).
✋️ 👆 🚫 🚫 ⚙️ 🎯 💽 🏷, 🎓 ⚖️ 🆎.
👆 💚 ✔️ `id` &amp; `email` &amp; 🚫 ✔️ 🙆 `username` 👆 🏷 ❓ 💭. 👆 💪 ⚙️ 👉 🎏 🧰.
👆 💚 ✔️ `str`❓ ⚖️ `dict`❓ ⚖️ 💽 🎓 🏷 👐 🔗 ❓ ⚫️ 🌐 👷 🎏 🌌.
👆 🤙 🚫 ✔️ 👩‍💻 👈 🕹 👆 🈸 ✋️ 🤖, 🤖, ⚖️ 🎏 ⚙️, 👈 ✔️ 🔐 🤝 ❓ 🔄, ⚫️ 🌐 👷 🎏.
⚙️ 🙆 😇 🏷, 🙆 😇 🎓, 🙆 😇 💽 👈 👆 💪 👆 🈸. **FastAPI** ✔️ 👆 📔 ⏮️ 🔗 💉 ⚙️.
## 📟 📐
👉 🖼 5⃣📆 😑 🔁. ✔️ 🤯 👈 👥 🌀 💂‍♂, 📊 🏷, 🚙 🔢 &amp; *➡ 🛠️* 🎏 📁.
✋️ 📥 🔑 ☝.
💂‍♂ &amp; 🔗 💉 💩 ✍ 🕐.
&amp; 👆 💪 ⚒ ⚫️ 🏗 👆 💚. &amp; , ✔️ ⚫️ ✍ 🕴 🕐, 👁 🥉. ⏮️ 🌐 💪.
✋️ 👆 💪 ✔️ 💯 🔗 (*➡ 🛠️*) ⚙️ 🎏 💂‍♂ ⚙️.
&amp; 🌐 👫 (⚖️ 🙆 ↔ 👫 👈 👆 💚) 💪 ✊ 📈 🏤-⚙️ 👫 🔗 ⚖️ 🙆 🎏 🔗 👆 ✍.
&amp; 🌐 👉 💯 *➡ 🛠️* 💪 🤪 3⃣ ⏸:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="30-32"
{!> ../../../docs_src/security/tutorial002.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="28-30"
{!> ../../../docs_src/security/tutorial002_py310.py!}
```
## 🌃
👆 💪 🔜 🤚 ⏮️ 👩‍💻 🔗 👆 *➡ 🛠️ 🔢*.
👥 ⏪ 😬 📤.
👥 💪 🚮 *➡ 🛠️* 👩‍💻/👩‍💻 🤙 📨 `username` &amp; `password`.
👈 👟 ⏭.

View File

@@ -0,0 +1,101 @@
# 💂‍♂ 🎶
📤 📚 🌌 🍵 💂‍♂, 🤝 &amp; ✔.
&amp; ⚫️ 🛎 🏗 &amp; "⚠" ❔.
📚 🛠️ &amp; ⚙️ 🍵 💂‍♂ &amp; 🤝 ✊ 🦏 💸 🎯 &amp; 📟 (📚 💼 ⚫️ 💪 5⃣0⃣ 💯 ⚖️ 🌅 🌐 📟 ✍).
**FastAPI** 🚚 📚 🧰 👆 🙅 ⏮️ **💂‍♂** 💪, 📉, 🐩 🌌, 🍵 ✔️ 🔬 &amp; 💡 🌐 💂‍♂ 🔧.
✋️ 🥇, ➡️ ✅ 🤪 🔧.
## 🏃 ❓
🚥 👆 🚫 💅 🔃 🙆 👉 ⚖ &amp; 👆 💪 🚮 💂‍♂ ⏮️ 🤝 ⚓️ 🔛 🆔 &amp; 🔐 *▶️️ 🔜*, 🚶 ⏭ 📃.
## Oauth2
Oauth2⃣ 🔧 👈 🔬 📚 🌌 🍵 🤝 &amp; ✔.
⚫️ 🔬 🔧 &amp; 📔 📚 🏗 ⚙️ 💼.
⚫️ 🔌 🌌 🔓 ⚙️ "🥉 🥳".
👈 ⚫️❔ 🌐 ⚙️ ⏮️ "💳 ⏮️ 👱📔, 🇺🇸🔍, 👱📔, 📂" ⚙️ 🔘.
### ✳ 1
📤 ✳ 1⃣, ❔ 📶 🎏 ⚪️➡️ Oauth2⃣, &amp; 🌖 🏗, ⚫️ 🔌 🔗 🔧 🔛 ❔ 🗜 📻.
⚫️ 🚫 📶 🌟 ⚖️ ⚙️ 🛎.
Oauth2⃣ 🚫 ✔ ❔ 🗜 📻, ⚫️ ⌛ 👆 ✔️ 👆 🈸 🍦 ⏮️ 🇺🇸🔍.
!!! tip
📄 🔃 **🛠️** 👆 🔜 👀 ❔ ⚒ 🆙 🇺🇸🔍 🆓, ⚙️ Traefik &amp; ➡️ 🗜.
## 👩‍💻 🔗
👩‍💻 🔗 1⃣ 🔧, 🧢 🔛 **Oauth2**.
⚫️ ↔ Oauth2⃣ ✔ 👜 👈 📶 🌌 Oauth2⃣, 🔄 ⚒ ⚫️ 🌅 🛠️.
🖼, 🇺🇸🔍 💳 ⚙️ 👩‍💻 🔗 (❔ 🔘 ⚙️ Oauth2⃣).
✋️ 👱📔 💳 🚫 🐕‍🦺 👩‍💻 🔗. ⚫️ ✔️ 🚮 👍 🍛 Oauth2⃣.
### 👩‍💻 (🚫 "👩‍💻 🔗")
📤 "👩‍💻" 🔧. 👈 🔄 ❎ 🎏 👜 **👩‍💻 🔗**, ✋️ 🚫 ⚓️ 🔛 Oauth2⃣.
, ⚫️ 🏁 🌖 ⚙️.
⚫️ 🚫 📶 🌟 ⚖️ ⚙️ 🛎.
## 🗄
🗄 (⏪ 💭 🦁) 📂 🔧 🏗 🔗 (🔜 🍕 💾 🏛).
**FastAPI** ⚓️ 🔛 **🗄**.
👈 ⚫️❔ ⚒ ⚫️ 💪 ✔️ 💗 🏧 🎓 🧾 🔢, 📟 ⚡, ♒️.
🗄 ✔️ 🌌 🔬 💗 💂‍♂ "⚖".
⚙️ 👫, 👆 💪 ✊ 📈 🌐 👫 🐩-⚓️ 🧰, 🔌 👉 🎓 🧾 ⚙️.
🗄 🔬 📄 💂‍♂ ⚖:
* `apiKey`: 🈸 🎯 🔑 👈 💪 👟 ⚪️➡️:
* 🔢 🔢.
* 🎚.
* 🍪.
* `http`: 🐩 🇺🇸🔍 🤝 ⚙️, 🔌:
* `bearer`: 🎚 `Authorization` ⏮️ 💲 `Bearer ` 🤝. 👉 😖 ⚪️➡️ Oauth2⃣.
* 🇺🇸🔍 🔰 🤝.
* 🇺🇸🔍 📰, ♒️.
* `oauth2`: 🌐 Oauth2⃣ 🌌 🍵 💂‍♂ (🤙 "💧").
* 📚 👫 💧 ☑ 🏗 ✳ 2⃣.0️⃣ 🤝 🐕‍🦺 (💖 🇺🇸🔍, 👱📔, 👱📔, 📂, ♒️):
* `implicit`
* `clientCredentials`
* `authorizationCode`
* ✋️ 📤 1⃣ 🎯 "💧" 👈 💪 👌 ⚙️ 🚚 🤝 🎏 🈸 🔗:
* `password`: ⏭ 📃 🔜 📔 🖼 👉.
* `openIdConnect`: ✔️ 🌌 🔬 ❔ 🔎 Oauth2⃣ 🤝 📊 🔁.
* 👉 🏧 🔍 ⚫️❔ 🔬 👩‍💻 🔗 🔧.
!!! tip
🛠️ 🎏 🤝/✔ 🐕‍🦺 💖 🇺🇸🔍, 👱📔, 👱📔, 📂, ♒️. 💪 &amp; 📶 ⏩.
🌅 🏗 ⚠ 🏗 🤝/✔ 🐕‍🦺 💖 👈, ✋️ **FastAPI** 🤝 👆 🧰 ⚫️ 💪, ⏪ 🔨 🏋️ 🏋‍♂ 👆.
## **FastAPI** 🚙
FastAPI 🚚 📚 🧰 🔠 👉 💂‍♂ ⚖ `fastapi.security` 🕹 👈 📉 ⚙️ 👉 💂‍♂ 🛠️.
⏭ 📃 👆 🔜 👀 ❔ 🚮 💂‍♂ 👆 🛠️ ⚙️ 📚 🧰 🚚 **FastAPI**.
&amp; 👆 🔜 👀 ❔ ⚫️ 🤚 🔁 🛠️ 🔘 🎓 🧾 ⚙️.

View File

@@ -0,0 +1,297 @@
# Oauth2⃣ ⏮️ 🔐 (&amp; 🔁), 📨 ⏮️ 🥙 🤝
🔜 👈 👥 ✔️ 🌐 💂‍♂ 💧, ➡️ ⚒ 🈸 🤙 🔐, ⚙️ <abbr title="JSON Web Tokens">🥙</abbr> 🤝 &amp; 🔐 🔐 🔁.
👉 📟 🕳 👆 💪 🤙 ⚙️ 👆 🈸, 🖊 🔐 #⃣ 👆 💽, ♒️.
👥 🔜 ▶️ ⚪️➡️ 🌐❔ 👥 ◀️ ⏮️ 📃 &amp; 📈 ⚫️.
## 🔃 🥙
🥙 ⛓ "🎻 🕸 🤝".
⚫️ 🐩 🚫 🎻 🎚 📏 💧 🎻 🍵 🚀. ⚫️ 👀 💖 👉:
```
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c
```
⚫️ 🚫 🗜,, 🙆 💪 🛡 ⚪️➡️ 🎚.
✋️ ⚫️ 🛑. , 🕐❔ 👆 📨 🤝 👈 👆 ♨, 👆 💪 ✔ 👈 👆 🤙 ♨ ⚫️.
👈 🌌, 👆 💪 ✍ 🤝 ⏮️ 👔, ➡️ 💬, 1⃣ 🗓️. &amp; ⤴️ 🕐❔ 👩‍💻 👟 🔙 ⏭ 📆 ⏮️ 🤝, 👆 💭 👈 👩‍💻 🕹 👆 ⚙️.
⏮️ 🗓️, 🤝 🔜 🕛 &amp; 👩‍💻 🔜 🚫 ✔ &amp; 🔜 ✔️ 🛑 🔄 🤚 🆕 🤝. &amp; 🚥 👩‍💻 (⚖️ 🥉 🥳) 🔄 🔀 🤝 🔀 👔, 👆 🔜 💪 🔎 ⚫️, ↩️ 💳 🔜 🚫 🏏.
🚥 👆 💚 🤾 ⏮️ 🥙 🤝 &amp; 👀 ❔ 👫 👷, ✅ <a href="https://jwt.io/" class="external-link" target="_blank">https://jwt.io</a>.
## ❎ `python-jose`
👥 💪 ❎ `python-jose` 🏗 &amp; ✔ 🥙 🤝 🐍:
<div class="termy">
```console
$ pip install "python-jose[cryptography]"
---> 100%
```
</div>
<a href="https://github.com/mpdavis/python-jose" class="external-link" target="_blank">🐍-🇩🇬</a> 🚚 🔐 👩‍💻 .
📥 👥 ⚙️ 👍 1⃣: <a href="https://cryptography.io/" class="external-link" target="_blank">)/⚛</a>.
!!! tip
👉 🔰 ⏪ ⚙️ <a href="https://pyjwt.readthedocs.io/" class="external-link" target="_blank">PyJWT</a>.
✋️ ⚫️ ⚙️ 🐍-🇩🇬 ↩️ ⚫️ 🚚 🌐 ⚒ ⚪️➡️ PyJWT 👈 👆 💪 💪 ⏪ 🕐❔ 🏗 🛠️ ⏮️ 🎏 🧰.
## 🔐 🔁
"🔁" ⛓ 🏭 🎚 (🔐 👉 💼) 🔘 🔁 🔢 (🎻) 👈 👀 💖 🙃.
🕐❔ 👆 🚶‍♀️ ⚫️❔ 🎏 🎚 (⚫️❔ 🎏 🔐) 👆 🤚 ⚫️❔ 🎏 🙃.
✋️ 👆 🚫🔜 🗜 ⚪️➡️ 🙃 🔙 🔐.
### ⚫️❔ ⚙️ 🔐 🔁
🚥 👆 💽 📎, 🧙‍♀ 🏆 🚫 ✔️ 👆 👩‍💻' 🔢 🔐, 🕴#⃣.
, 🧙‍♀ 🏆 🚫 💪 🔄 ⚙️ 👈 🔐 1⃣ ⚙️ (📚 👩‍💻 ⚙️ 🎏 🔐 🌐, 👉 🔜 ⚠).
## ❎ `passlib`
🇸🇲 👑 🐍 📦 🍵 🔐#⃣.
⚫️ 🐕‍🦺 📚 🔐 🔁 📊 &amp; 🚙 👷 ⏮️ 👫.
👍 📊 "🐡".
, ❎ 🇸🇲 ⏮️ 🐡:
<div class="termy">
```console
$ pip install "passlib[bcrypt]"
---> 100%
```
</div>
!!! tip
⏮️ `passlib`, 👆 💪 🔗 ⚫️ 💪 ✍ 🔐 ✍ **✳**, **🏺** 💂‍♂ 🔌-⚖️ 📚 🎏.
, 👆 🔜 💪, 🖼, 💰 🎏 📊 ⚪️➡️ ✳ 🈸 💽 ⏮️ FastAPI 🈸. ⚖️ 📉 ↔ ✳ 🈸 ⚙️ 🎏 💽.
&amp; 👆 👩‍💻 🔜 💪 💳 ⚪️➡️ 👆 ✳ 📱 ⚖️ ⚪️➡️ 👆 **FastAPI** 📱, 🎏 🕰.
## #️⃣ &amp; ✔ 🔐
🗄 🧰 👥 💪 ⚪️➡️ `passlib`.
✍ 🇸🇲 "🔑". 👉 ⚫️❔ 🔜 ⚙️ #⃣ &amp; ✔ 🔐.
!!! tip
🇸🇲 🔑 ✔️ 🛠️ ⚙️ 🎏 🔁 📊, 🔌 😢 🗝 🕐 🕴 ✔ ✔ 👫, ♒️.
🖼, 👆 💪 ⚙️ ⚫️ ✍ &amp; ✔ 🔐 🏗 1⃣ ⚙️ (💖 ✳) ✋️ #⃣ 🙆 🆕 🔐 ⏮️ 🎏 📊 💖 🐡.
&amp; 🔗 ⏮️ 🌐 👫 🎏 🕰.
✍ 🚙 🔢 #⃣ 🔐 👟 ⚪️➡️ 👩‍💻.
&amp; 1⃣ 🚙 ✔ 🚥 📨 🔐 🏏 #⃣ 🏪.
&amp; 1⃣ 1⃣ 🔓 &amp; 📨 👩‍💻.
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="7 48 55-56 59-60 69-75"
{!> ../../../docs_src/security/tutorial004.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="6 47 54-55 58-59 68-74"
{!> ../../../docs_src/security/tutorial004_py310.py!}
```
!!! note
🚥 👆 ✅ 🆕 (❌) 💽 `fake_users_db`, 👆 🔜 👀 ❔ #️⃣ 🔐 👀 💖 🔜: `"$2b$12$EixZaYVK1fsbw1ZfbX3OXePaWxn96p36WQoeG6Lruj3vjPGga31lW"`.
## 🍵 🥙 🤝
🗄 🕹 ❎.
✍ 🎲 ㊙ 🔑 👈 🔜 ⚙️ 🛑 🥙 🤝.
🏗 🔐 🎲 ㊙ 🔑 ⚙️ 📋:
<div class="termy">
```console
$ openssl rand -hex 32
09d25e094faa6ca2556c818166b7a9563b93f7099f6f0f4caa6cf63b88e8d3e7
```
</div>
&amp; 📁 🔢 🔢 `SECRET_KEY` (🚫 ⚙️ 1⃣ 🖼).
✍ 🔢 `ALGORITHM` ⏮️ 📊 ⚙️ 🛑 🥙 🤝 &amp; ⚒ ⚫️ `"HS256"`.
✍ 🔢 👔 🤝.
🔬 Pydantic 🏷 👈 🔜 ⚙️ 🤝 🔗 📨.
✍ 🚙 🔢 🏗 🆕 🔐 🤝.
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="6 12-14 28-30 78-86"
{!> ../../../docs_src/security/tutorial004.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="5 11-13 27-29 77-85"
{!> ../../../docs_src/security/tutorial004_py310.py!}
```
## 🔗
`get_current_user` 📨 🎏 🤝 ⏭, ✋️ 👉 🕰, ⚙️ 🥙 🤝.
🔣 📨 🤝, ✔ ⚫️, &amp; 📨 ⏮️ 👩‍💻.
🚥 🤝 ❌, 📨 🇺🇸🔍 ❌ ▶️️ ↖️.
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="89-106"
{!> ../../../docs_src/security/tutorial004.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="88-105"
{!> ../../../docs_src/security/tutorial004_py310.py!}
```
## `/token` *➡ 🛠️*
✍ `timedelta` ⏮️ 👔 🕰 🤝.
✍ 🎰 🥙 🔐 🤝 &amp; 📨 ⚫️.
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="115-128"
{!> ../../../docs_src/security/tutorial004.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="114-127"
{!> ../../../docs_src/security/tutorial004_py310.py!}
```
### 📡 🔃 🥙 "📄" `sub`
🥙 🔧 💬 👈 📤 🔑 `sub`, ⏮️ 📄 🤝.
⚫️ 📦 ⚙️ ⚫️, ✋️ 👈 🌐❔ 👆 🔜 🚮 👩‍💻 🆔, 👥 ⚙️ ⚫️ 📥.
🥙 5⃣📆 ⚙️ 🎏 👜 ↖️ ⚪️➡️ ⚖ 👩‍💻 &amp; 🤝 👫 🎭 🛠️ 🔗 🔛 👆 🛠️.
🖼, 👆 💪 🔬 "🚘" ⚖️ "📰 🏤".
⤴️ 👆 💪 🚮 ✔ 🔃 👈 👨‍💼, 💖 "💾" (🚘) ⚖️ "✍" (📰).
&amp; ⤴️, 👆 💪 🤝 👈 🥙 🤝 👩‍💻 (⚖️ 🤖), &amp; 👫 💪 ⚙️ ⚫️ 🎭 👈 🎯 (💾 🚘, ⚖️ ✍ 📰 🏤) 🍵 💆‍♂ ✔️ 🏧, ⏮️ 🥙 🤝 👆 🛠️ 🏗 👈.
⚙️ 👫 💭, 🥙 💪 ⚙️ 🌌 🌖 🤓 😐.
📚 💼, 📚 👈 👨‍💼 💪 ✔️ 🎏 🆔, ➡️ 💬 `foo` (👩‍💻 `foo`, 🚘 `foo`, &amp; 📰 🏤 `foo`).
, ❎ 🆔 💥, 🕐❔ 🏗 🥙 🤝 👩‍💻, 👆 💪 🔡 💲 `sub` 🔑, ✅ ⏮️ `username:`. , 👉 🖼, 💲 `sub` 💪 ✔️: `username:johndoe`.
⚠ 👜 ✔️ 🤯 👈 `sub` 🔑 🔜 ✔️ 😍 🆔 🤭 🎂 🈸, &amp; ⚫️ 🔜 🎻.
## ✅ ⚫️
🏃 💽 &amp; 🚶 🩺: <a href="http://127.0.0.1:8000/docs" class="external-link" target="_blank">http://127.0.0.1:8000/docs</a>.
👆 🔜 👀 👩‍💻 🔢 💖:
<img src="/img/tutorial/security/image07.png">
✔ 🈸 🎏 🌌 ⏭.
⚙️ 🎓:
🆔: `johndoe`
🔐: `secret`
!!! check
👀 👈 🕳 📟 🔢 🔐 "`secret`", 👥 🕴 ✔️ #️⃣ ⏬.
<img src="/img/tutorial/security/image08.png">
🤙 🔗 `/users/me/`, 👆 🔜 🤚 📨:
```JSON
{
"username": "johndoe",
"email": "johndoe@example.com",
"full_name": "John Doe",
"disabled": false
}
```
<img src="/img/tutorial/security/image09.png">
🚥 👆 📂 👩‍💻 🧰, 👆 💪 👀 ❔ 📊 📨 🕴 🔌 🤝, 🔐 🕴 📨 🥇 📨 🔓 👩‍💻 &amp; 🤚 👈 🔐 🤝, ✋️ 🚫 ⏮️:
<img src="/img/tutorial/security/image10.png">
!!! note
👀 🎚 `Authorization`, ⏮️ 💲 👈 ▶️ ⏮️ `Bearer `.
## 🏧 ⚙️ ⏮️ `scopes`
Oauth2⃣ ✔️ 🔑 "↔".
👆 💪 ⚙️ 👫 🚮 🎯 ⚒ ✔ 🥙 🤝.
⤴️ 👆 💪 🤝 👉 🤝 👩‍💻 🔗 ⚖️ 🥉 🥳, 🔗 ⏮️ 👆 🛠️ ⏮️ ⚒ 🚫.
👆 💪 💡 ❔ ⚙️ 👫 &amp; ❔ 👫 🛠️ 🔘 **FastAPI** ⏪ **🏧 👩‍💻 🦮**.
## 🌃
⏮️ ⚫️❔ 👆 ✔️ 👀 🆙 🔜, 👆 💪 ⚒ 🆙 🔐 **FastAPI** 🈸 ⚙️ 🐩 💖 Oauth2⃣ &amp; 🥙.
🌖 🙆 🛠️ 🚚 💂‍♂ ▶️️ 👍 🏗 📄 🔜.
📚 📦 👈 📉 ⚫️ 📚 ✔️ ⚒ 📚 ⚠ ⏮️ 💽 🏷, 💽, &amp; 💪 ⚒. &amp; 👉 📦 👈 📉 👜 💁‍♂️ 🌅 🤙 ✔️ 💂‍♂ ⚠ 🔘.
---
**FastAPI** 🚫 ⚒ 🙆 ⚠ ⏮️ 🙆 💽, 💽 🏷 ⚖️ 🧰.
⚫️ 🤝 👆 🌐 💪 ⚒ 🕐 👈 👖 👆 🏗 🏆.
&amp; 👆 💪 ⚙️ 🔗 📚 👍 🚧 &amp; 🛎 ⚙️ 📦 💖 `passlib` &amp; `python-jose`, ↩️ **FastAPI** 🚫 🚚 🙆 🏗 🛠️ 🛠️ 🔢 📦.
✋️ ⚫️ 🚚 👆 🧰 📉 🛠️ 🌅 💪 🍵 🎯 💪, ⚖, ⚖️ 💂‍♂.
&amp; 👆 💪 ⚙️ &amp; 🛠️ 🔐, 🐩 🛠️, 💖 Oauth2⃣ 📶 🙅 🌌.
👆 💪 💡 🌅 **🏧 👩‍💻 🦮** 🔃 ❔ ⚙️ Oauth2⃣ "↔", 🌖 👌-🧽 ✔ ⚙️, 📄 👫 🎏 🐩. Oauth2⃣ ⏮️ ↔ 🛠️ ⚙️ 📚 🦏 🤝 🐕‍🦺, 💖 👱📔, 🇺🇸🔍, 📂, 🤸‍♂, 👱📔, ♒️. ✔ 🥉 🥳 🈸 🔗 ⏮️ 👫 🔗 🔛 👨‍💼 👫 👩‍💻.

View File

@@ -0,0 +1,315 @@
# 🙅 Oauth2⃣ ⏮️ 🔐 &amp; 📨
🔜 ➡️ 🏗 ⚪️➡️ ⏮️ 📃 &amp; 🚮 ❌ 🍕 ✔️ 🏁 💂‍♂ 💧.
## 🤚 `username` &amp; `password`
👥 🔜 ⚙️ **FastAPI** 💂‍♂ 🚙 🤚 `username` &amp; `password`.
Oauth2⃣ ✔ 👈 🕐❔ ⚙️ "🔐 💧" (👈 👥 ⚙️) 👩‍💻/👩‍💻 🔜 📨 `username` &amp; `password` 🏑 📨 💽.
&amp; 🔌 💬 👈 🏑 ✔️ 🌟 💖 👈. `user-name` ⚖️ `email` 🚫🔜 👷.
✋️ 🚫 😟, 👆 💪 🎦 ⚫️ 👆 🎋 👆 🏁 👩‍💻 🕸.
&amp; 👆 💽 🏷 💪 ⚙️ 🙆 🎏 📛 👆 💚.
✋️ 💳 *➡ 🛠️*, 👥 💪 ⚙️ 👉 📛 🔗 ⏮️ 🔌 (&amp; 💪, 🖼, ⚙️ 🛠️ 🛠️ 🧾 ⚙️).
🔌 🇵🇸 👈 `username` &amp; `password` 🔜 📨 📨 💽 (, 🙅‍♂ 🎻 📥).
### `scope`
🔌 💬 👈 👩‍💻 💪 📨 1⃣ 📨 🏑 "`scope`".
📨 🏑 📛 `scope` (⭐), ✋️ ⚫️ 🤙 📏 🎻 ⏮️ "↔" 🎏 🚀.
🔠 "↔" 🎻 (🍵 🚀).
👫 🛎 ⚙️ 📣 🎯 💂‍♂ ✔, 🖼:
* `users:read` ⚖️ `users:write` ⚠ 🖼.
* `instagram_basic` ⚙️ 👱📔 / 👱📔.
* `https://www.googleapis.com/auth/drive` ⚙️ 🇺🇸🔍.
!!! info
Oauth2⃣ "↔" 🎻 👈 📣 🎯 ✔ ✔.
⚫️ 🚫 🤔 🚥 ⚫️ ✔️ 🎏 🦹 💖 `:` ⚖️ 🚥 ⚫️ 📛.
👈 🛠️ 🎯.
Oauth2⃣ 👫 🎻.
## 📟 🤚 `username` &amp; `password`
🔜 ➡️ ⚙️ 🚙 🚚 **FastAPI** 🍵 👉.
### `OAuth2PasswordRequestForm`
🥇, 🗄 `OAuth2PasswordRequestForm`, &amp; ⚙️ ⚫️ 🔗 ⏮️ `Depends` *➡ 🛠️* `/token`:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="4 76"
{!> ../../../docs_src/security/tutorial003.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="2 74"
{!> ../../../docs_src/security/tutorial003_py310.py!}
```
`OAuth2PasswordRequestForm` 🎓 🔗 👈 📣 📨 💪 ⏮️:
* `username`.
* `password`.
* 📦 `scope` 🏑 🦏 🎻, ✍ 🎻 🎏 🚀.
* 📦 `grant_type`.
!!! tip
Oauth2⃣ 🔌 🤙 *🚚* 🏑 `grant_type` ⏮️ 🔧 💲 `password`, ✋️ `OAuth2PasswordRequestForm` 🚫 🛠️ ⚫️.
🚥 👆 💪 🛠️ ⚫️, ⚙️ `OAuth2PasswordRequestFormStrict` ↩️ `OAuth2PasswordRequestForm`.
* 📦 `client_id` (👥 🚫 💪 ⚫️ 👆 🖼).
* 📦 `client_secret` (👥 🚫 💪 ⚫️ 👆 🖼).
!!! info
`OAuth2PasswordRequestForm` 🚫 🎁 🎓 **FastAPI** `OAuth2PasswordBearer`.
`OAuth2PasswordBearer` ⚒ **FastAPI** 💭 👈 ⚫️ 💂‍♂ ⚖. ⚫️ 🚮 👈 🌌 🗄.
✋️ `OAuth2PasswordRequestForm` 🎓 🔗 👈 👆 💪 ✔️ ✍ 👆, ⚖️ 👆 💪 ✔️ 📣 `Form` 🔢 🔗.
✋️ ⚫️ ⚠ ⚙️ 💼, ⚫️ 🚚 **FastAPI** 🔗, ⚒ ⚫️ ⏩.
### ⚙️ 📨 💽
!!! tip
👐 🔗 🎓 `OAuth2PasswordRequestForm` 🏆 🚫 ✔️ 🔢 `scope` ⏮️ 📏 🎻 👽 🚀, ↩️, ⚫️ 🔜 ✔️ `scopes` 🔢 ⏮️ ☑ 📇 🎻 🔠 ↔ 📨.
👥 🚫 ⚙️ `scopes` 👉 🖼, ✋️ 🛠️ 📤 🚥 👆 💪 ⚫️.
🔜, 🤚 👩‍💻 📊 ⚪️➡️ (❌) 💽, ⚙️ `username` ⚪️➡️ 📨 🏑.
🚥 📤 🙅‍♂ ✅ 👩‍💻, 👥 📨 ❌ 💬 "❌ 🆔 ⚖️ 🔐".
❌, 👥 ⚙️ ⚠ `HTTPException`:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="3 77-79"
{!> ../../../docs_src/security/tutorial003.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="1 75-77"
{!> ../../../docs_src/security/tutorial003_py310.py!}
```
### ✅ 🔐
👉 ☝ 👥 ✔️ 👩‍💻 📊 ⚪️➡️ 👆 💽, ✋️ 👥 🚫 ✅ 🔐.
➡️ 🚮 👈 💽 Pydantic `UserInDB` 🏷 🥇.
👆 🔜 🙅 🖊 🔢 🔐,, 👥 🔜 ⚙️ (❌) 🔐 🔁 ⚙️.
🚥 🔐 🚫 🏏, 👥 📨 🎏 ❌.
#### 🔐 🔁
"🔁" ⛓: 🏭 🎚 (🔐 👉 💼) 🔘 🔁 🔢 (🎻) 👈 👀 💖 🙃.
🕐❔ 👆 🚶‍♀️ ⚫️❔ 🎏 🎚 (⚫️❔ 🎏 🔐) 👆 🤚 ⚫️❔ 🎏 🙃.
✋️ 👆 🚫🔜 🗜 ⚪️➡️ 🙃 🔙 🔐.
##### ⚫️❔ ⚙️ 🔐 🔁
🚥 👆 💽 📎, 🧙‍♀ 🏆 🚫 ✔️ 👆 👩‍💻' 🔢 🔐, 🕴#️⃣.
, 🧙‍♀ 🏆 🚫 💪 🔄 ⚙️ 👈 🎏 🔐 1⃣ ⚙️ (📚 👩‍💻 ⚙️ 🎏 🔐 🌐, 👉 🔜 ⚠).
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="80-83"
{!> ../../../docs_src/security/tutorial003.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="78-81"
{!> ../../../docs_src/security/tutorial003_py310.py!}
```
#### 🔃 `**user_dict`
`UserInDB(**user_dict)` ⛓:
*🚶‍♀️ 🔑 &amp; 💲 `user_dict` 🔗 🔑-💲 ❌, 🌓:*
```Python
UserInDB(
username = user_dict["username"],
email = user_dict["email"],
full_name = user_dict["full_name"],
disabled = user_dict["disabled"],
hashed_password = user_dict["hashed_password"],
)
```
!!! info
🌅 🏁 🔑 `**👩💻_ #` ✅ 🔙 [🧾 ** 🏷**](../extra-models.md#about-user_indict){.internal-link target=_blank}.
## 📨 🤝
📨 `token` 🔗 🔜 🎻 🎚.
⚫️ 🔜 ✔️ `token_type`. 👆 💼, 👥 ⚙️ "📨" 🤝, 🤝 🆎 🔜 "`bearer`".
&amp; ⚫️ 🔜 ✔️ `access_token`, ⏮️ 🎻 ⚗ 👆 🔐 🤝.
👉 🙅 🖼, 👥 🔜 🍕 😟 &amp; 📨 🎏 `username` 🤝.
!!! tip
⏭ 📃, 👆 🔜 👀 🎰 🔐 🛠️, ⏮️ 🔐 #️⃣ &amp; <abbr title="JSON Web Tokens">🥙</abbr> 🤝.
✋️ 🔜, ➡️ 🎯 🔛 🎯 👥 💪.
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="85"
{!> ../../../docs_src/security/tutorial003.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="83"
{!> ../../../docs_src/security/tutorial003_py310.py!}
```
!!! tip
🔌, 👆 🔜 📨 🎻 ⏮️ `access_token` &amp; `token_type`, 🎏 👉 🖼.
👉 🕳 👈 👆 ✔️ 👆 👆 📟, &amp; ⚒ 💭 👆 ⚙️ 📚 🎻 🔑.
⚫️ 🌖 🕴 👜 👈 👆 ✔️ 💭 ☑ 👆, 🛠️ ⏮️ 🔧.
🎂, **FastAPI** 🍵 ⚫️ 👆.
## 🔗
🔜 👥 🔜 👆 🔗.
👥 💚 🤚 `current_user` *🕴* 🚥 👉 👩‍💻 🦁.
, 👥 ✍ 🌖 🔗 `get_current_active_user` 👈 🔄 ⚙️ `get_current_user` 🔗.
👯‍♂️ 👉 🔗 🔜 📨 🇺🇸🔍 ❌ 🚥 👩‍💻 🚫 🔀, ⚖️ 🚥 🔕.
, 👆 🔗, 👥 🔜 🕴 🤚 👩‍💻 🚥 👩‍💻 🔀, ☑ 🔓, &amp; 🦁:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="58-66 69-72 90"
{!> ../../../docs_src/security/tutorial003.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="55-64 67-70 88"
{!> ../../../docs_src/security/tutorial003_py310.py!}
```
!!! info
🌖 🎚 `WWW-Authenticate` ⏮️ 💲 `Bearer` 👥 🛬 📥 🍕 🔌.
🙆 🇺🇸🔍 (❌) 👔 📟 4⃣0⃣1⃣ "⛔" 🤔 📨 `WWW-Authenticate` 🎚.
💼 📨 🤝 (👆 💼), 💲 👈 🎚 🔜 `Bearer`.
👆 💪 🤙 🚶 👈 🎚 &amp; ⚫️ 🔜 👷.
✋️ ⚫️ 🚚 📥 🛠️ ⏮️ 🔧.
, 📤 5⃣📆 🧰 👈 ⌛ &amp; ⚙️ ⚫️ (🔜 ⚖️ 🔮) &amp; 👈 💪 ⚠ 👆 ⚖️ 👆 👩‍💻, 🔜 ⚖️ 🔮.
👈 💰 🐩...
## 👀 ⚫️ 🎯
📂 🎓 🩺: <a href="http://127.0.0.1:8000/docs" class="external-link" target="_blank">http://127.0.0.1:8000/docs</a>.
### 🔓
🖊 "✔" 🔼.
⚙️ 🎓:
👩‍💻: `johndoe`
🔐: `secret`
<img src="/img/tutorial/security/image04.png">
⏮️ 🔗 ⚙️, 👆 🔜 👀 ⚫️ 💖:
<img src="/img/tutorial/security/image05.png">
### 🤚 👆 👍 👩‍💻 💽
🔜 ⚙️ 🛠️ `GET` ⏮️ ➡ `/users/me`.
👆 🔜 🤚 👆 👩‍💻 📊, 💖:
```JSON
{
"username": "johndoe",
"email": "johndoe@example.com",
"full_name": "John Doe",
"disabled": false,
"hashed_password": "fakehashedsecret"
}
```
<img src="/img/tutorial/security/image06.png">
🚥 👆 🖊 🔒 &amp; ⏏, &amp; ⤴️ 🔄 🎏 🛠️ 🔄, 👆 🔜 🤚 🇺🇸🔍 4⃣0⃣1⃣ ❌:
```JSON
{
"detail": "Not authenticated"
}
```
### 🔕 👩‍💻
🔜 🔄 ⏮️ 🔕 👩‍💻, 🔓 ⏮️:
👩‍💻: `alice`
🔐: `secret2`
&amp; 🔄 ⚙️ 🛠️ `GET` ⏮️ ➡ `/users/me`.
👆 🔜 🤚 "🔕 👩‍💻" ❌, 💖:
```JSON
{
"detail": "Inactive user"
}
```
## 🌃
👆 🔜 ✔️ 🧰 🛠️ 🏁 💂‍♂ ⚙️ ⚓️ 🔛 `username` &amp; `password` 👆 🛠️.
⚙️ 👫 🧰, 👆 💪 ⚒ 💂‍♂ ⚙️ 🔗 ⏮️ 🙆 💽 &amp; ⏮️ 🙆 👩‍💻 ⚖️ 💽 🏷.
🕴 ❌ 👈 ⚫️ 🚫 🤙 "🔐".
⏭ 📃 👆 🔜 👀 ❔ ⚙️ 🔐 🔐 🔁 🗃 &amp; <abbr title="JSON Web Tokens">🥙</abbr> 🤝.

View File

@@ -0,0 +1,786 @@
# 🗄 (🔗) 💽
**FastAPI** 🚫 🚚 👆 ⚙️ 🗄 (🔗) 💽.
✋️ 👆 💪 ⚙️ 🙆 🔗 💽 👈 👆 💚.
📥 👥 🔜 👀 🖼 ⚙️ <a href="https://www.sqlalchemy.org/" class="external-link" target="_blank">🇸🇲</a>.
👆 💪 💪 🛠️ ⚫️ 🙆 💽 🐕‍🦺 🇸🇲, 💖:
*
*
* 🗄
* 🐸
* 🤸‍♂ 🗄 💽, ♒️.
👉 🖼, 👥 🔜 ⚙️ **🗄**, ↩️ ⚫️ ⚙️ 👁 📁 &amp; 🐍 ✔️ 🛠️ 🐕‍🦺. , 👆 💪 📁 👉 🖼 &amp; 🏃 ⚫️.
⏪, 👆 🏭 🈸, 👆 💪 💚 ⚙️ 💽 💽 💖 **✳**.
!!! tip
📤 🛂 🏗 🚂 ⏮️ **FastAPI** &amp; **✳**, 🌐 ⚓️ 🔛 **☁**, 🔌 🕸 &amp; 🌖 🧰: <a href="https://github.com/tiangolo/full-stack-fastapi-postgresql" class="external-link" target="_blank">https://github.com/tiangolo/full-stack-fastapi-postgresql</a>
!!! note
👀 👈 📚 📟 🐩 `SQLAlchemy` 📟 👆 🔜 ⚙️ ⏮️ 🙆 🛠️.
**FastAPI** 🎯 📟 🤪 🕧.
## 🐜
**FastAPI** 👷 ⏮️ 🙆 💽 &amp; 🙆 👗 🗃 💬 💽.
⚠ ⚓ ⚙️ "🐜": "🎚-🔗 🗺" 🗃.
🐜 ✔️ 🧰 🗜 ("*🗺*") 🖖 *🎚* 📟 &amp; 💽 🏓 ("*🔗*").
⏮️ 🐜, 👆 🛎 ✍ 🎓 👈 🎨 🏓 🗄 💽, 🔠 🔢 🎓 🎨 🏓, ⏮️ 📛 &amp; 🆎.
🖼 🎓 `Pet` 💪 🎨 🗄 🏓 `pets`.
&amp; 🔠 *👐* 🎚 👈 🎓 🎨 ⏭ 💽.
🖼 🎚 `orion_cat` (👐 `Pet`) 💪 ✔️ 🔢 `orion_cat.type`, 🏓 `type`. &amp; 💲 👈 🔢 💪, ✅ `"cat"`.
👫 🐜 ✔️ 🧰 ⚒ 🔗 ⚖️ 🔗 🖖 🏓 ⚖️ 👨‍💼.
👉 🌌, 👆 💪 ✔️ 🔢 `orion_cat.owner` &amp; 👨‍💼 🔜 🔌 💽 👉 🐶 👨‍💼, ✊ ⚪️➡️ 🏓 *👨‍💼*.
, `orion_cat.owner.name` 💪 📛 (⚪️➡️ `name` 🏓 `owners` 🏓) 👉 🐶 👨‍💼.
⚫️ 💪 ✔️ 💲 💖 `"Arquilian"`.
&amp; 🐜 🔜 🌐 👷 🤚 ⚪️➡️ 🔗 🏓 *👨‍💼* 🕐❔ 👆 🔄 🔐 ⚫️ ⚪️➡️ 👆 🐶 🎚.
⚠ 🐜 🖼: ✳-🐜 (🍕 ✳ 🛠️), 🇸🇲 🐜 (🍕 🇸🇲, 🔬 🛠️) &amp; 🏒 (🔬 🛠️), 👪 🎏.
📥 👥 🔜 👀 ❔ 👷 ⏮️ **🇸🇲 🐜**.
🎏 🌌 👆 💪 ⚙️ 🙆 🎏 🐜.
!!! tip
📤 🌓 📄 ⚙️ 🏒 📥 🩺.
## 📁 📊
👫 🖼, ➡️ 💬 👆 ✔️ 📁 📛 `my_super_project` 👈 🔌 🎧-📁 🤙 `sql_app` ⏮️ 📊 💖 👉:
```
.
└── sql_app
├── __init__.py
├── crud.py
├── database.py
├── main.py
├── models.py
└── schemas.py
```
📁 `__init__.py` 🛁 📁, ✋️ ⚫️ 💬 🐍 👈 `sql_app` ⏮️ 🌐 🚮 🕹 (🐍 📁) 📦.
🔜 ➡️ 👀 ⚫️❔ 🔠 📁/🕹 🔨.
## ❎ `SQLAlchemy`
🥇 👆 💪 ❎ `SQLAlchemy`:
<div class="termy">
```console
$ pip install sqlalchemy
---> 100%
```
</div>
## ✍ 🇸🇲 🍕
➡️ 🔗 📁 `sql_app/database.py`.
### 🗄 🇸🇲 🍕
```Python hl_lines="1-3"
{!../../../docs_src/sql_databases/sql_app/database.py!}
```
### ✍ 💽 📛 🇸🇲
```Python hl_lines="5-6"
{!../../../docs_src/sql_databases/sql_app/database.py!}
```
👉 🖼, 👥 "🔗" 🗄 💽 (📂 📁 ⏮️ 🗄 💽).
📁 🔜 🔎 🎏 📁 📁 `sql_app.db`.
👈 ⚫️❔ 🏁 🍕 `./sql_app.db`.
🚥 👆 ⚙️ **✳** 💽 ↩️, 👆 🔜 ✔️ ✍ ⏸:
```Python
SQLALCHEMY_DATABASE_URL = "postgresql://user:password@postgresserver/db"
```
...&amp; 🛠️ ⚫️ ⏮️ 👆 💽 📊 &amp; 🎓 (📊 ✳, ✳ ⚖️ 🙆 🎏).
!!! tip
👉 👑 ⏸ 👈 👆 🔜 ✔️ 🔀 🚥 👆 💚 ⚙️ 🎏 💽.
### ✍ 🇸🇲 `engine`
🥇 🔁 ✍ 🇸🇲 "🚒".
👥 🔜 ⏪ ⚙️ 👉 `engine` 🎏 🥉.
```Python hl_lines="8-10"
{!../../../docs_src/sql_databases/sql_app/database.py!}
```
#### 🗒
❌:
```Python
connect_args={"check_same_thread": False}
```
...💪 🕴 `SQLite`. ⚫️ 🚫 💪 🎏 💽.
!!! info "📡 "
🔢 🗄 🔜 🕴 ✔ 1⃣ 🧵 🔗 ⏮️ ⚫️, 🤔 👈 🔠 🧵 🔜 🍵 🔬 📨.
👉 ❎ 😫 🤝 🎏 🔗 🎏 👜 (🎏 📨).
✋️ FastAPI, ⚙️ 😐 🔢 (`def`) 🌅 🌘 1⃣ 🧵 💪 🔗 ⏮️ 💽 🎏 📨, 👥 💪 ⚒ 🗄 💭 👈 ⚫️ 🔜 ✔ 👈 ⏮️ `connect_args={"check_same_thread": False}`.
, 👥 🔜 ⚒ 💭 🔠 📨 🤚 🚮 👍 💽 🔗 🎉 🔗, 📤 🙅‍♂ 💪 👈 🔢 🛠️.
### ✍ `SessionLocal` 🎓
🔠 👐 `SessionLocal` 🎓 🔜 💽 🎉. 🎓 ⚫️ 🚫 💽 🎉.
✋️ 🕐 👥 ✍ 👐 `SessionLocal` 🎓, 👉 👐 🔜 ☑ 💽 🎉.
👥 📛 ⚫️ `SessionLocal` 🔬 ⚫️ ⚪️➡️ `Session` 👥 🏭 ⚪️➡️ 🇸🇲.
👥 🔜 ⚙️ `Session` (1⃣ 🗄 ⚪️➡️ 🇸🇲) ⏪.
✍ `SessionLocal` 🎓, ⚙️ 🔢 `sessionmaker`:
```Python hl_lines="11"
{!../../../docs_src/sql_databases/sql_app/database.py!}
```
### ✍ `Base` 🎓
🔜 👥 🔜 ⚙️ 🔢 `declarative_base()` 👈 📨 🎓.
⏪ 👥 🔜 😖 ⚪️➡️ 👉 🎓 ✍ 🔠 💽 🏷 ⚖️ 🎓 (🐜 🏷):
```Python hl_lines="13"
{!../../../docs_src/sql_databases/sql_app/database.py!}
```
## ✍ 💽 🏷
➡️ 🔜 👀 📁 `sql_app/models.py`.
### ✍ 🇸🇲 🏷 ⚪️➡️ `Base` 🎓
👥 🔜 ⚙️ 👉 `Base` 🎓 👥 ✍ ⏭ ✍ 🇸🇲 🏷.
!!! tip
🇸🇲 ⚙️ ⚖ "**🏷**" 🔗 👉 🎓 &amp; 👐 👈 🔗 ⏮️ 💽.
✋️ Pydantic ⚙️ ⚖ "**🏷**" 🔗 🕳 🎏, 💽 🔬, 🛠️, &amp; 🧾 🎓 &amp; 👐.
🗄 `Base` ⚪️➡️ `database` (📁 `database.py` ⚪️➡️ 🔛).
✍ 🎓 👈 😖 ⚪️➡️ ⚫️.
👫 🎓 🇸🇲 🏷.
```Python hl_lines="4 7-8 18-19"
{!../../../docs_src/sql_databases/sql_app/models.py!}
```
`__tablename__` 🔢 💬 🇸🇲 📛 🏓 ⚙️ 💽 🔠 👫 🏷.
### ✍ 🏷 🔢/🏓
🔜 ✍ 🌐 🏷 (🎓) 🔢.
🔠 👫 🔢 🎨 🏓 🚮 🔗 💽 🏓.
👥 ⚙️ `Column` ⚪️➡️ 🇸🇲 🔢 💲.
&amp; 👥 🚶‍♀️ 🇸🇲 🎓 "🆎", `Integer`, `String`, &amp; `Boolean`, 👈 🔬 🆎 💽, ❌.
```Python hl_lines="1 10-13 21-24"
{!../../../docs_src/sql_databases/sql_app/models.py!}
```
### ✍ 💛
🔜 ✍ 💛.
👉, 👥 ⚙️ `relationship` 🚚 🇸🇲 🐜.
👉 🔜 ▶️️, 🌅 ⚖️ 🌘, "🎱" 🔢 👈 🔜 🔌 💲 ⚪️➡️ 🎏 🏓 🔗 👉 1⃣.
```Python hl_lines="2 15 26"
{!../../../docs_src/sql_databases/sql_app/models.py!}
```
🕐❔ 🔐 🔢 `items` `User`, `my_user.items`, ⚫️ 🔜 ✔️ 📇 `Item` 🇸🇲 🏷 (⚪️➡️ `items` 🏓) 👈 ✔️ 💱 🔑 ☝ 👉 ⏺ `users` 🏓.
🕐❔ 👆 🔐 `my_user.items`, 🇸🇲 🔜 🤙 🚶 &amp; ☕ 🏬 ⚪️➡️ 💽 `items` 🏓 &amp; 🔗 👫 📥.
&amp; 🕐❔ 🔐 🔢 `owner` `Item`, ⚫️ 🔜 🔌 `User` 🇸🇲 🏷 ⚪️➡️ `users` 🏓. ⚫️ 🔜 ⚙️ `owner_id` 🔢/🏓 ⏮️ 🚮 💱 🔑 💭 ❔ ⏺ 🤚 ⚪️➡️ `users` 🏓.
## ✍ Pydantic 🏷
🔜 ➡️ ✅ 📁 `sql_app/schemas.py`.
!!! tip
❎ 😨 🖖 🇸🇲 *🏷* &amp; Pydantic *🏷*, 👥 🔜 ✔️ 📁 `models.py` ⏮️ 🇸🇲 🏷, &amp; 📁 `schemas.py` ⏮️ Pydantic 🏷.
👫 Pydantic 🏷 🔬 🌅 ⚖️ 🌘 "🔗" (☑ 📊 💠).
👉 🔜 👥 ❎ 😨 ⏪ ⚙️ 👯‍♂️.
### ✍ ▶️ Pydantic *🏷* / 🔗
✍ `ItemBase` &amp; `UserBase` Pydantic *🏷* (⚖️ ➡️ 💬 "🔗") ✔️ ⚠ 🔢 ⏪ 🏗 ⚖️ 👂 📊.
&amp; ✍ `ItemCreate` &amp; `UserCreate` 👈 😖 ⚪️➡️ 👫 (👫 🔜 ✔️ 🎏 🔢), 🙆 🌖 📊 (🔢) 💪 🏗.
, 👩‍💻 🔜 ✔️ `password` 🕐❔ 🏗 ⚫️.
✋️ 💂‍♂, `password` 🏆 🚫 🎏 Pydantic *🏷*, 🖼, ⚫️ 🏆 🚫 📨 ⚪️➡️ 🛠️ 🕐❔ 👂 👩‍💻.
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="3 6-8 11-12 23-24 27-28"
{!> ../../../docs_src/sql_databases/sql_app/schemas.py!}
```
=== "🐍 3⃣.9️⃣ &amp; 🔛"
```Python hl_lines="3 6-8 11-12 23-24 27-28"
{!> ../../../docs_src/sql_databases/sql_app_py39/schemas.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="1 4-6 9-10 21-22 25-26"
{!> ../../../docs_src/sql_databases/sql_app_py310/schemas.py!}
```
#### 🇸🇲 👗 &amp; Pydantic 👗
👀 👈 🇸🇲 *🏷* 🔬 🔢 ⚙️ `=`, &amp; 🚶‍♀️ 🆎 🔢 `Column`, 💖:
```Python
name = Column(String)
```
⏪ Pydantic *🏷* 📣 🆎 ⚙️ `:`, 🆕 🆎 ✍ ❕/🆎 🔑:
```Python
name: str
```
✔️ ⚫️ 🤯, 👆 🚫 🤚 😕 🕐❔ ⚙️ `=` &amp; `:` ⏮️ 👫.
### ✍ Pydantic *🏷* / 🔗 👂 / 📨
🔜 ✍ Pydantic *🏷* (🔗) 👈 🔜 ⚙️ 🕐❔ 👂 💽, 🕐❔ 🛬 ⚫️ ⚪️➡️ 🛠️.
🖼, ⏭ 🏗 🏬, 👥 🚫 💭 ⚫️❔ 🔜 🆔 🛠️ ⚫️, ✋️ 🕐❔ 👂 ⚫️ (🕐❔ 🛬 ⚫️ ⚪️➡️ 🛠️) 👥 🔜 ⏪ 💭 🚮 🆔.
🎏 🌌, 🕐❔ 👂 👩‍💻, 👥 💪 🔜 📣 👈 `items` 🔜 🔌 🏬 👈 💭 👉 👩‍💻.
🚫 🕴 🆔 📚 🏬, ✋️ 🌐 💽 👈 👥 🔬 Pydantic *🏷* 👂 🏬: `Item`.
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="15-17 31-34"
{!> ../../../docs_src/sql_databases/sql_app/schemas.py!}
```
=== "🐍 3⃣.9️⃣ &amp; 🔛"
```Python hl_lines="15-17 31-34"
{!> ../../../docs_src/sql_databases/sql_app_py39/schemas.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="13-15 29-32"
{!> ../../../docs_src/sql_databases/sql_app_py310/schemas.py!}
```
!!! tip
👀 👈 `User`, Pydantic *🏷* 👈 🔜 ⚙️ 🕐❔ 👂 👩‍💻 (🛬 ⚫️ ⚪️➡️ 🛠️) 🚫 🔌 `password`.
### ⚙️ Pydantic `orm_mode`
🔜, Pydantic *🏷* 👂, `Item` &amp; `User`, 🚮 🔗 `Config` 🎓.
👉 <a href="https://pydantic-docs.helpmanual.io/usage/model_config/" class="external-link" target="_blank">`Config`</a> 🎓 ⚙️ 🚚 📳 Pydantic.
`Config` 🎓, ⚒ 🔢 `orm_mode = True`.
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="15 19-20 31 36-37"
{!> ../../../docs_src/sql_databases/sql_app/schemas.py!}
```
=== "🐍 3⃣.9️⃣ &amp; 🔛"
```Python hl_lines="15 19-20 31 36-37"
{!> ../../../docs_src/sql_databases/sql_app_py39/schemas.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python hl_lines="13 17-18 29 34-35"
{!> ../../../docs_src/sql_databases/sql_app_py310/schemas.py!}
```
!!! tip
👀 ⚫️ ⚖ 💲 ⏮️ `=`, 💖:
`orm_mode = True`
⚫️ 🚫 ⚙️ `:` 🆎 📄 ⏭.
👉 ⚒ 📁 💲, 🚫 📣 🆎.
Pydantic `orm_mode` 🔜 💬 Pydantic *🏷* ✍ 💽 🚥 ⚫️ 🚫 `dict`, ✋️ 🐜 🏷 (⚖️ 🙆 🎏 ❌ 🎚 ⏮️ 🔢).
👉 🌌, ↩️ 🕴 🔄 🤚 `id` 💲 ⚪️➡️ `dict`,:
```Python
id = data["id"]
```
⚫️ 🔜 🔄 🤚 ⚫️ ⚪️➡️ 🔢,:
```Python
id = data.id
```
&amp; ⏮️ 👉, Pydantic *🏷* 🔗 ⏮️ 🐜, &amp; 👆 💪 📣 ⚫️ `response_model` ❌ 👆 *➡ 🛠️*.
👆 🔜 💪 📨 💽 🏷 &amp; ⚫️ 🔜 ✍ 💽 ⚪️➡️ ⚫️.
#### 📡 🔃 🐜 📳
🇸🇲 &amp; 📚 🎏 🔢 "🙃 🚚".
👈 ⛓, 🖼, 👈 👫 🚫 ☕ 💽 💛 ⚪️➡️ 💽 🚥 👆 🔄 🔐 🔢 👈 🔜 🔌 👈 💽.
🖼, 🔐 🔢 `items`:
```Python
current_user.items
```
🔜 ⚒ 🇸🇲 🚶 `items` 🏓 &amp; 🤚 🏬 👉 👩‍💻, ✋️ 🚫 ⏭.
🍵 `orm_mode`, 🚥 👆 📨 🇸🇲 🏷 ⚪️➡️ 👆 *➡ 🛠️*, ⚫️ 🚫🔜 🔌 💛 💽.
🚥 👆 📣 📚 💛 👆 Pydantic 🏷.
✋️ ⏮️ 🐜 📳, Pydantic ⚫️ 🔜 🔄 🔐 💽 ⚫️ 💪 ⚪️➡️ 🔢 (↩️ 🤔 `dict`), 👆 💪 📣 🎯 💽 👆 💚 📨 &amp; ⚫️ 🔜 💪 🚶 &amp; 🤚 ⚫️, ⚪️➡️ 🐜.
## 💩 🇨🇻
🔜 ➡️ 👀 📁 `sql_app/crud.py`.
👉 📁 👥 🔜 ✔️ ♻ 🔢 🔗 ⏮️ 💽 💽.
**💩** 👟 ⚪️➡️: **🅱**📧, **Ⓜ**💳, **👤** = , &amp; **🇨🇮**📧.
...👐 👉 🖼 👥 🕴 🏗 &amp; 👂.
### ✍ 💽
🗄 `Session` ⚪️➡️ `sqlalchemy.orm`, 👉 🔜 ✔ 👆 📣 🆎 `db` 🔢 &amp; ✔️ 👻 🆎 ✅ &amp; 🛠️ 👆 🔢.
🗄 `models` (🇸🇲 🏷) &amp; `schemas` (Pydantic *🏷* / 🔗).
✍ 🚙 🔢:
* ✍ 👁 👩‍💻 🆔 &amp; 📧.
* ✍ 💗 👩‍💻.
* ✍ 💗 🏬.
```Python hl_lines="1 3 6-7 10-11 14-15 27-28"
{!../../../docs_src/sql_databases/sql_app/crud.py!}
```
!!! tip
🏗 🔢 👈 🕴 💡 🔗 ⏮️ 💽 (🤚 👩‍💻 ⚖️ 🏬) 🔬 👆 *➡ 🛠️ 🔢*, 👆 💪 🌖 💪 ♻ 👫 💗 🍕 &amp; 🚮 <abbr title="Automated tests, written in code, that check if another piece of code is working correctly.">⚒ 💯</abbr> 👫.
### ✍ 💽
🔜 ✍ 🚙 🔢 ✍ 💽.
🔁:
* ✍ 🇸🇲 🏷 *👐* ⏮️ 👆 📊.
* `add` 👈 👐 🎚 👆 💽 🎉.
* `commit` 🔀 💽 (👈 👫 🖊).
* `refresh` 👆 👐 (👈 ⚫️ 🔌 🙆 🆕 📊 ⚪️➡️ 💽, 💖 🏗 🆔).
```Python hl_lines="18-24 31-36"
{!../../../docs_src/sql_databases/sql_app/crud.py!}
```
!!! tip
🇸🇲 🏷 `User` 🔌 `hashed_password` 👈 🔜 🔌 🔐 #️⃣ ⏬ 🔐.
✋️ ⚫️❔ 🛠️ 👩‍💻 🚚 ⏮️ 🔐, 👆 💪 ⚗ ⚫️ &amp; 🏗 #️⃣ 🔐 👆 🈸.
&amp; ⤴️ 🚶‍♀️ `hashed_password` ❌ ⏮️ 💲 🖊.
!!! warning
👉 🖼 🚫 🔐, 🔐 🚫#️⃣.
🎰 👨‍❤‍👨 🈸 👆 🔜 💪 #️⃣ 🔐 &amp; 🙅 🖊 👫 🔢.
🌅 , 🚶 🔙 💂‍♂ 📄 🔰.
📥 👥 🎯 🕴 🔛 🧰 &amp; 👨‍🔧 💽.
!!! tip
↩️ 🚶‍♀️ 🔠 🇨🇻 ❌ `Item` &amp; 👂 🔠 1⃣ 👫 ⚪️➡️ Pydantic *🏷*, 👥 🏭 `dict` ⏮️ Pydantic *🏷*'Ⓜ 📊 ⏮️:
`item.dict()`
&amp; ⤴️ 👥 🚶‍♀️ `dict`'Ⓜ 🔑-💲 👫 🇨🇻 ❌ 🇸🇲 `Item`, ⏮️:
`Item(**item.dict())`
&amp; ⤴️ 👥 🚶‍♀️ 🇨🇻 ❌ `owner_id` 👈 🚫 🚚 Pydantic *🏷*, ⏮️:
`Item(**item.dict(), owner_id=user_id)`
## 👑 **FastAPI** 📱
&amp; 🔜 📁 `sql_app/main.py` ➡️ 🛠️ &amp; ⚙️ 🌐 🎏 🍕 👥 ✍ ⏭.
### ✍ 💽 🏓
📶 🙃 🌌 ✍ 💽 🏓:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="9"
{!> ../../../docs_src/sql_databases/sql_app/main.py!}
```
=== "🐍 3⃣.9️⃣ &amp; 🔛"
```Python hl_lines="7"
{!> ../../../docs_src/sql_databases/sql_app_py39/main.py!}
```
#### ⚗ 🗒
🛎 👆 🔜 🎲 🔢 👆 💽 (✍ 🏓, ♒️) ⏮️ <a href="https://alembic.sqlalchemy.org/en/latest/" class="external-link" target="_blank">⚗</a>.
&amp; 👆 🔜 ⚙️ ⚗ "🛠️" (👈 🚮 👑 👨‍🏭).
"🛠️" ⚒ 🔁 💪 🕐❔ 👆 🔀 📊 👆 🇸🇲 🏷, 🚮 🆕 🔢, ♒️. 🔁 👈 🔀 💽, 🚮 🆕 🏓, 🆕 🏓, ♒️.
👆 💪 🔎 🖼 ⚗ FastAPI 🏗 📄 ⚪️➡️ [🏗 ⚡ - 📄](../project-generation.md){.internal-link target=_blank}. 🎯 <a href="https://github.com/tiangolo/full-stack-fastapi-postgresql/tree/master/%7B%7Bcookiecutter.project_slug%7D%7D/backend/app/alembic/" class="external-link" target="_blank"> `alembic` 📁 📟</a>.
### ✍ 🔗
🔜 ⚙️ `SessionLocal` 🎓 👥 ✍ `sql_app/database.py` 📁 ✍ 🔗.
👥 💪 ✔️ 🔬 💽 🎉/🔗 (`SessionLocal`) 📍 📨, ⚙️ 🎏 🎉 🔘 🌐 📨 &amp; ⤴️ 🔐 ⚫️ ⏮️ 📨 🏁.
&amp; ⤴️ 🆕 🎉 🔜 ✍ ⏭ 📨.
👈, 👥 🔜 ✍ 🆕 🔗 ⏮️ `yield`, 🔬 ⏭ 📄 🔃 [🔗 ⏮️ `yield`](dependencies/dependencies-with-yield.md){.internal-link target=_blank}.
👆 🔗 🔜 ✍ 🆕 🇸🇲 `SessionLocal` 👈 🔜 ⚙️ 👁 📨, &amp; ⤴️ 🔐 ⚫️ 🕐 📨 🏁.
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="15-20"
{!> ../../../docs_src/sql_databases/sql_app/main.py!}
```
=== "🐍 3⃣.9️⃣ &amp; 🔛"
```Python hl_lines="13-18"
{!> ../../../docs_src/sql_databases/sql_app_py39/main.py!}
```
!!! info
👥 🚮 🏗 `SessionLocal()` &amp; 🚚 📨 `try` 🍫.
&amp; ⤴️ 👥 🔐 ⚫️ `finally` 🍫.
👉 🌌 👥 ⚒ 💭 💽 🎉 🕧 📪 ⏮️ 📨. 🚥 📤 ⚠ ⏪ 🏭 📨.
✋️ 👆 💪 🚫 🤚 1⃣ ⚠ ⚪️➡️ 🚪 📟 (⏮️ `yield`). 👀 🌖 [🔗 ⏮️ `yield` &amp; `HTTPException`](./dependencies/dependencies-with-yield.md#dependencies-with-yield-and-httpexception){.internal-link target=_blank}
&amp; ⤴️, 🕐❔ ⚙️ 🔗 *➡ 🛠️ 🔢*, 👥 📣 ⚫️ ⏮️ 🆎 `Session` 👥 🗄 🔗 ⚪️➡️ 🇸🇲.
👉 🔜 ⤴️ 🤝 👥 👍 👨‍🎨 🐕‍🦺 🔘 *➡ 🛠️ 🔢*, ↩️ 👨‍🎨 🔜 💭 👈 `db` 🔢 🆎 `Session`:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="24 32 38 47 53"
{!> ../../../docs_src/sql_databases/sql_app/main.py!}
```
=== "🐍 3⃣.9️⃣ &amp; 🔛"
```Python hl_lines="22 30 36 45 51"
{!> ../../../docs_src/sql_databases/sql_app_py39/main.py!}
```
!!! info "📡 "
🔢 `db` 🤙 🆎 `SessionLocal`, ✋️ 👉 🎓 (✍ ⏮️ `sessionmaker()`) "🗳" 🇸🇲 `Session`,, 👨‍🎨 🚫 🤙 💭 ⚫️❔ 👩‍🔬 🚚.
✋️ 📣 🆎 `Session`, 👨‍🎨 🔜 💪 💭 💪 👩‍🔬 (`.add()`, `.query()`, `.commit()`, ♒️) &amp; 💪 🚚 👍 🐕‍🦺 (💖 🛠️). 🆎 📄 🚫 📉 ☑ 🎚.
### ✍ 👆 **FastAPI** *➡ 🛠️*
🔜, 😒, 📥 🐩 **FastAPI** *➡ 🛠️* 📟.
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="23-28 31-34 37-42 45-49 52-55"
{!> ../../../docs_src/sql_databases/sql_app/main.py!}
```
=== "🐍 3⃣.9️⃣ &amp; 🔛"
```Python hl_lines="21-26 29-32 35-40 43-47 50-53"
{!> ../../../docs_src/sql_databases/sql_app_py39/main.py!}
```
👥 🏗 💽 🎉 ⏭ 🔠 📨 🔗 ⏮️ `yield`, &amp; ⤴️ 📪 ⚫️ ⏮️.
&amp; ⤴️ 👥 💪 ✍ 🚚 🔗 *➡ 🛠️ 🔢*, 🤚 👈 🎉 🔗.
⏮️ 👈, 👥 💪 🤙 `crud.get_user` 🔗 ⚪️➡️ 🔘 *➡ 🛠️ 🔢* &amp; ⚙️ 👈 🎉.
!!! tip
👀 👈 💲 👆 📨 🇸🇲 🏷, ⚖️ 📇 🇸🇲 🏷.
✋️ 🌐 *➡ 🛠️* ✔️ `response_model` ⏮️ Pydantic *🏷* / 🔗 ⚙️ `orm_mode`, 💽 📣 👆 Pydantic 🏷 🔜 ⚗ ⚪️➡️ 👫 &amp; 📨 👩‍💻, ⏮️ 🌐 😐 ⛽ &amp; 🔬.
!!! tip
👀 👈 📤 `response_models` 👈 ✔️ 🐩 🐍 🆎 💖 `List[schemas.Item]`.
✋️ 🎚/🔢 👈 `List` Pydantic *🏷* ⏮️ `orm_mode`, 💽 🔜 🗃 &amp; 📨 👩‍💻 🛎, 🍵 ⚠.
### 🔃 `def` 🆚 `async def`
📥 👥 ⚙️ 🇸🇲 📟 🔘 *➡ 🛠️ 🔢* &amp; 🔗, &amp;, 🔄, ⚫️ 🔜 🚶 &amp; 🔗 ⏮️ 🔢 💽.
👈 💪 ⚠ 🚚 "⌛".
✋️ 🇸🇲 🚫 ✔️ 🔗 ⚙️ `await` 🔗, 🔜 ⏮️ 🕳 💖:
```Python
user = await db.query(User).first()
```
...&amp; ↩️ 👥 ⚙️:
```Python
user = db.query(User).first()
```
⤴️ 👥 🔜 📣 *➡ 🛠️ 🔢* &amp; 🔗 🍵 `async def`, ⏮️ 😐 `def`,:
```Python hl_lines="2"
@app.get("/users/{user_id}", response_model=schemas.User)
def read_user(user_id: int, db: Session = Depends(get_db)):
db_user = crud.get_user(db, user_id=user_id)
...
```
!!! info
🚥 👆 💪 🔗 👆 🔗 💽 🔁, 👀 [🔁 🗄 (🔗) 💽](../advanced/async-sql-databases.md){.internal-link target=_blank}.
!!! note "📶 📡 "
🚥 👆 😟 &amp; ✔️ ⏬ 📡 💡, 👆 💪 ✅ 📶 📡 ❔ 👉 `async def` 🆚 `def` 🍵 [🔁](../async.md#very-technical-details){.internal-link target=_blank} 🩺.
## 🛠️
↩️ 👥 ⚙️ 🇸🇲 🔗 &amp; 👥 🚫 🚚 🙆 😇 🔌-⚫️ 👷 ⏮️ **FastAPI**, 👥 💪 🛠️ 💽 <abbr title="Automatically updating the database to have any new column we define in our models.">🛠️</abbr> ⏮️ <a href="https://alembic.sqlalchemy.org" class="external-link" target="_blank">⚗</a> 🔗.
&amp; 📟 🔗 🇸🇲 &amp; 🇸🇲 🏷 🖖 🎏 🔬 📁, 👆 🔜 💪 🎭 🛠️ ⏮️ ⚗ 🍵 ✔️ ❎ FastAPI, Pydantic, ⚖️ 🕳 🙆.
🎏 🌌, 👆 🔜 💪 ⚙️ 🎏 🇸🇲 🏷 &amp; 🚙 🎏 🍕 👆 📟 👈 🚫 🔗 **FastAPI**.
🖼, 🖥 📋 👨‍🏭 ⏮️ <a href="https://docs.celeryq.dev" class="external-link" target="_blank">🥒</a>, <a href="https://python-rq.org/" class="external-link" target="_blank">🅿</a>, ⚖️ <a href="https://arq-docs.helpmanual.io/" class="external-link" target="_blank">📶</a>.
## 📄 🌐 📁
💭 👆 🔜 ✔️ 📁 📛 `my_super_project` 👈 🔌 🎧-📁 🤙 `sql_app`.
`sql_app` 🔜 ✔️ 📄 📁:
* `sql_app/__init__.py`: 🛁 📁.
* `sql_app/database.py`:
```Python
{!../../../docs_src/sql_databases/sql_app/database.py!}
```
* `sql_app/models.py`:
```Python
{!../../../docs_src/sql_databases/sql_app/models.py!}
```
* `sql_app/schemas.py`:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python
{!> ../../../docs_src/sql_databases/sql_app/schemas.py!}
```
=== "🐍 3⃣.9️⃣ &amp; 🔛"
```Python
{!> ../../../docs_src/sql_databases/sql_app_py39/schemas.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python
{!> ../../../docs_src/sql_databases/sql_app_py310/schemas.py!}
```
* `sql_app/crud.py`:
```Python
{!../../../docs_src/sql_databases/sql_app/crud.py!}
```
* `sql_app/main.py`:
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python
{!> ../../../docs_src/sql_databases/sql_app/main.py!}
```
=== "🐍 3⃣.9️⃣ &amp; 🔛"
```Python
{!> ../../../docs_src/sql_databases/sql_app_py39/main.py!}
```
## ✅ ⚫️
👆 💪 📁 👉 📟 &amp; ⚙️ ⚫️.
!!! info
👐, 📟 🎦 📥 🍕 💯. 🌅 📟 👉 🩺.
⤴️ 👆 💪 🏃 ⚫️ ⏮️ Uvicorn:
<div class="termy">
```console
$ uvicorn sql_app.main:app --reload
<span style="color: green;">INFO</span>: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
```
</div>
&amp; ⤴️, 👆 💪 📂 👆 🖥 <a href="http://127.0.0.1:8000/docs" class="external-link" target="_blank">http://127.0.0.1:8000/docs</a>.
&amp; 👆 🔜 💪 🔗 ⏮️ 👆 **FastAPI** 🈸, 👂 📊 ⚪️➡️ 🎰 💽:
<img src="/img/tutorial/sql-databases/image01.png">
## 🔗 ⏮️ 💽 🔗
🚥 👆 💚 🔬 🗄 💽 (📁) 🔗, ➡ FastAPI, 🚮 🎚, 🚮 🏓, 🏓, ⏺, 🔀 📊, ♒️. 👆 💪 ⚙️ <a href="https://sqlitebrowser.org/" class="external-link" target="_blank">💽 🖥 🗄</a>.
⚫️ 🔜 👀 💖 👉:
<img src="/img/tutorial/sql-databases/image02.png">
👆 💪 ⚙️ 💳 🗄 🖥 💖 <a href="https://inloop.github.io/sqlite-viewer/" class="external-link" target="_blank">🗄 📋</a> ⚖️ <a href="https://extendsclass.com/sqlite-browser.html" class="external-link" target="_blank">ExtendsClass</a>.
## 🎛 💽 🎉 ⏮️ 🛠️
🚥 👆 💪 🚫 ⚙️ 🔗 ⏮️ `yield` - 🖼, 🚥 👆 🚫 ⚙️ **🐍 3⃣.7️⃣** &amp; 💪 🚫 ❎ "🐛" 🤔 🔛 **🐍 3⃣.6️⃣** - 👆 💪 ⚒ 🆙 🎉 "🛠️" 🎏 🌌.
"🛠️" 🌖 🔢 👈 🕧 🛠️ 🔠 📨, ⏮️ 📟 🛠️ ⏭, &amp; 📟 🛠️ ⏮️ 🔗 🔢.
### ✍ 🛠️
🛠️ 👥 🔜 🚮 (🔢) 🔜 ✍ 🆕 🇸🇲 `SessionLocal` 🔠 📨, 🚮 ⚫️ 📨 &amp; ⤴️ 🔐 ⚫️ 🕐 📨 🏁.
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python hl_lines="14-22"
{!> ../../../docs_src/sql_databases/sql_app/alt_main.py!}
```
=== "🐍 3⃣.9️⃣ &amp; 🔛"
```Python hl_lines="12-20"
{!> ../../../docs_src/sql_databases/sql_app_py39/alt_main.py!}
```
!!! info
👥 🚮 🏗 `SessionLocal()` &amp; 🚚 📨 `try` 🍫.
&amp; ⤴️ 👥 🔐 ⚫️ `finally` 🍫.
👉 🌌 👥 ⚒ 💭 💽 🎉 🕧 📪 ⏮️ 📨. 🚥 📤 ⚠ ⏪ 🏭 📨.
### 🔃 `request.state`
`request.state` 🏠 🔠 `Request` 🎚. ⚫️ 📤 🏪 ❌ 🎚 📎 📨 ⚫️, 💖 💽 🎉 👉 💼. 👆 💪 ✍ 🌅 🔃 ⚫️ <a href="https://www.starlette.io/requests/#other-state" class="external-link" target="_blank">💃 🩺 🔃 `Request` 🇵🇸</a>.
👥 👉 💼, ⚫️ 👥 🚚 👁 💽 🎉 ⚙️ 🔘 🌐 📨, &amp; ⤴️ 🔐 ⏮️ (🛠️).
### 🔗 ⏮️ `yield` ⚖️ 🛠️
❎ **🛠️** 📥 🎏 ⚫️❔ 🔗 ⏮️ `yield` 🔨, ⏮️ 🔺:
* ⚫️ 🚚 🌖 📟 &amp; 👄 🌅 🏗.
* 🛠️ ✔️ `async` 🔢.
* 🚥 📤 📟 ⚫️ 👈 ✔️ "⌛" 🕸, ⚫️ 💪 "🍫" 👆 🈸 📤 &amp; 📉 🎭 🍖.
* 👐 ⚫️ 🎲 🚫 📶 ⚠ 📥 ⏮️ 🌌 `SQLAlchemy` 👷.
* ✋️ 🚥 👆 🚮 🌖 📟 🛠️ 👈 ✔️ 📚 <abbr title="input and output">👤/🅾</abbr> ⌛, ⚫️ 💪 ⤴️ ⚠.
* 🛠️ 🏃 *🔠* 📨.
* , 🔗 🔜 ✍ 🔠 📨.
* 🕐❔ *➡ 🛠️* 👈 🍵 👈 📨 🚫 💪 💽.
!!! tip
⚫️ 🎲 👍 ⚙️ 🔗 ⏮️ `yield` 🕐❔ 👫 🥃 ⚙️ 💼.
!!! info
🔗 ⏮️ `yield` 🚮 ⏳ **FastAPI**.
⏮️ ⏬ 👉 🔰 🕴 ✔️ 🖼 ⏮️ 🛠️ &amp; 📤 🎲 📚 🈸 ⚙️ 🛠️ 💽 🎉 🧾.

View File

@@ -0,0 +1,39 @@
# 🎻 📁
👆 💪 🍦 🎻 📁 🔁 ⚪️➡️ 📁 ⚙️ `StaticFiles`.
## ⚙️ `StaticFiles`
* 🗄 `StaticFiles`.
* "🗻" `StaticFiles()` 👐 🎯 ➡.
```Python hl_lines="2 6"
{!../../../docs_src/static_files/tutorial001.py!}
```
!!! note "📡 "
👆 💪 ⚙️ `from starlette.staticfiles import StaticFiles`.
**FastAPI** 🚚 🎏 `starlette.staticfiles` `fastapi.staticfiles` 🏪 👆, 👩‍💻. ✋️ ⚫️ 🤙 👟 🔗 ⚪️➡️ 💃.
### ⚫️❔ "🗜"
"🗜" ⛓ ❎ 🏁 "🔬" 🈸 🎯 ➡, 👈 ⤴️ ✊ 💅 🚚 🌐 🎧-➡.
👉 🎏 ⚪️➡️ ⚙️ `APIRouter` 🗻 🈸 🍕 🔬. 🗄 &amp; 🩺 ⚪️➡️ 👆 👑 🈸 🏆 🚫 🔌 🕳 ⚪️➡️ 🗻 🈸, ♒️.
👆 💪 ✍ 🌅 🔃 👉 **🏧 👩‍💻 🦮**.
##
🥇 `"/static"` 🔗 🎧-➡ 👉 "🎧-🈸" 🔜 "🗻" 🔛. , 🙆 ➡ 👈 ▶️ ⏮️ `"/static"` 🔜 🍵 ⚫️.
`directory="static"` 🔗 📛 📁 👈 🔌 👆 🎻 📁.
`name="static"` 🤝 ⚫️ 📛 👈 💪 ⚙️ 🔘 **FastAPI**.
🌐 👫 🔢 💪 🎏 🌘 "`static`", 🔆 👫 ⏮️ 💪 &amp; 🎯 👆 👍 🈸.
## 🌅
🌖 &amp; 🎛 ✅ <a href="https://www.starlette.io/staticfiles/" class="external-link" target="_blank">💃 🩺 🔃 🎻 📁</a>.

View File

@@ -0,0 +1,188 @@
# 🔬
👏 <a href="https://www.starlette.io/testclient/" class="external-link" target="_blank">💃</a>, 🔬 **FastAPI** 🈸 ⏩ &amp; 😌.
⚫️ ⚓️ 🔛 <a href="https://www.python-httpx.org" class="external-link" target="_blank">🇸🇲</a>, ❔ 🔄 🏗 ⚓️ 🔛 📨, ⚫️ 📶 😰 &amp; 🏋️.
⏮️ ⚫️, 👆 💪 ⚙️ <a href="https://docs.pytest.org/" class="external-link" target="_blank">✳</a> 🔗 ⏮️ **FastAPI**.
## ⚙️ `TestClient`
!!! info
⚙️ `TestClient`, 🥇 ❎ <a href="https://www.python-httpx.org" class="external-link" target="_blank">`httpx`</a>.
🤶 Ⓜ. `pip install httpx`.
🗄 `TestClient`.
`TestClient` 🚶‍♀️ 👆 **FastAPI** 🈸 ⚫️.
✍ 🔢 ⏮️ 📛 👈 ▶️ ⏮️ `test_` (👉 🐩 `pytest` 🏛).
⚙️ `TestClient` 🎚 🎏 🌌 👆 ⏮️ `httpx`.
✍ 🙅 `assert` 📄 ⏮️ 🐩 🐍 🧬 👈 👆 💪 ✅ (🔄, 🐩 `pytest`).
```Python hl_lines="2 12 15-18"
{!../../../docs_src/app_testing/tutorial001.py!}
```
!!! tip
👀 👈 🔬 🔢 😐 `def`, 🚫 `async def`.
&amp; 🤙 👩‍💻 😐 🤙, 🚫 ⚙️ `await`.
👉 ✔ 👆 ⚙️ `pytest` 🔗 🍵 🤢.
!!! note "📡 "
👆 💪 ⚙️ `from starlette.testclient import TestClient`.
**FastAPI** 🚚 🎏 `starlette.testclient` `fastapi.testclient` 🏪 👆, 👩‍💻. ✋️ ⚫️ 👟 🔗 ⚪️➡️ 💃.
!!! tip
🚥 👆 💚 🤙 `async` 🔢 👆 💯 ↖️ ⚪️➡️ 📨 📨 👆 FastAPI 🈸 (✅ 🔁 💽 🔢), ✔️ 👀 [🔁 💯](../advanced/async-tests.md){.internal-link target=_blank} 🏧 🔰.
## 🎏 💯
🎰 🈸, 👆 🎲 🔜 ✔️ 👆 💯 🎏 📁.
&amp; 👆 **FastAPI** 🈸 5⃣📆 ✍ 📚 📁/🕹, ♒️.
### **FastAPI** 📱 📁
➡️ 💬 👆 ✔️ 📁 📊 🔬 [🦏 🈸](./bigger-applications.md){.internal-link target=_blank}:
```
.
├── app
│   ├── __init__.py
│   └── main.py
```
📁 `main.py` 👆 ✔️ 👆 **FastAPI** 📱:
```Python
{!../../../docs_src/app_testing/main.py!}
```
### 🔬 📁
⤴️ 👆 💪 ✔️ 📁 `test_main.py` ⏮️ 👆 💯. ⚫️ 💪 🖖 🔛 🎏 🐍 📦 (🎏 📁 ⏮️ `__init__.py` 📁):
``` hl_lines="5"
.
├── app
│   ├── __init__.py
│   ├── main.py
│   └── test_main.py
```
↩️ 👉 📁 🎏 📦, 👆 💪 ⚙️ ⚖ 🗄 🗄 🎚 `app` ⚪️➡️ `main` 🕹 (`main.py`):
```Python hl_lines="3"
{!../../../docs_src/app_testing/test_main.py!}
```
...&amp; ✔️ 📟 💯 💖 ⏭.
## 🔬: ↔ 🖼
🔜 ➡️ ↔ 👉 🖼 &amp; 🚮 🌖 👀 ❔ 💯 🎏 🍕.
### ↔ **FastAPI** 📱 📁
➡️ 😣 ⏮️ 🎏 📁 📊 ⏭:
```
.
├── app
│   ├── __init__.py
│   ├── main.py
│   └── test_main.py
```
➡️ 💬 👈 🔜 📁 `main.py` ⏮️ 👆 **FastAPI** 📱 ✔️ 🎏 **➡ 🛠️**.
⚫️ ✔️ `GET` 🛠️ 👈 💪 📨 ❌.
⚫️ ✔️ `POST` 🛠️ 👈 💪 📨 📚 ❌.
👯‍♂️ *➡ 🛠️* 🚚 `X-Token` 🎚.
=== "🐍 3⃣.6️⃣ &amp; 🔛"
```Python
{!> ../../../docs_src/app_testing/app_b/main.py!}
```
=== "🐍 3⃣.1⃣0⃣ &amp; 🔛"
```Python
{!> ../../../docs_src/app_testing/app_b_py310/main.py!}
```
### ↔ 🔬 📁
👆 💪 ⤴️ `test_main.py` ⏮️ ↔ 💯:
```Python
{!> ../../../docs_src/app_testing/app_b/test_main.py!}
```
🕐❔ 👆 💪 👩‍💻 🚶‍♀️ 📨 &amp; 👆 🚫 💭 ❔, 👆 💪 🔎 (🇺🇸🔍) ❔ ⚫️ `httpx`, ⚖️ ❔ ⚫️ ⏮️ `requests`, 🇸🇲 🔧 ⚓️ 🔛 📨' 🔧.
⤴️ 👆 🎏 👆 💯.
🤶 Ⓜ.:
* 🚶‍♀️ *➡* ⚖️ *🔢* 🔢, 🚮 ⚫️ 📛 ⚫️.
* 🚶‍♀️ 🎻 💪, 🚶‍♀️ 🐍 🎚 (✅ `dict`) 🔢 `json`.
* 🚥 👆 💪 📨 *📨 💽* ↩️ 🎻, ⚙️ `data` 🔢 ↩️.
* 🚶‍♀️ *🎚*, ⚙️ `dict` `headers` 🔢.
* *🍪*, `dict` `cookies` 🔢.
🌖 🔃 ❔ 🚶‍♀️ 💽 👩‍💻 (⚙️ `httpx` ⚖️ `TestClient`) ✅ <a href="https://www.python-httpx.org" class="external-link" target="_blank">🇸🇲 🧾</a>.
!!! info
🗒 👈 `TestClient` 📨 💽 👈 💪 🗜 🎻, 🚫 Pydantic 🏷.
🚥 👆 ✔️ Pydantic 🏷 👆 💯 &amp; 👆 💚 📨 🚮 💽 🈸 ⏮️ 🔬, 👆 💪 ⚙️ `jsonable_encoder` 🔬 [🎻 🔗 🔢](encoder.md){.internal-link target=_blank}.
## 🏃 ⚫️
⏮️ 👈, 👆 💪 ❎ `pytest`:
<div class="termy">
```console
$ pip install pytest
---> 100%
```
</div>
⚫️ 🔜 🔍 📁 &amp; 💯 🔁, 🛠️ 👫, &amp; 📄 🏁 🔙 👆.
🏃 💯 ⏮️:
<div class="termy">
```console
$ pytest
================ test session starts ================
platform linux -- Python 3.6.9, pytest-5.3.5, py-1.8.1, pluggy-0.13.1
rootdir: /home/user/code/superawesome-cli/app
plugins: forked-1.1.3, xdist-1.31.0, cov-2.8.1
collected 6 items
---> 100%
test_main.py <span style="color: green; white-space: pre;">...... [100%]</span>
<span style="color: green;">================= 1 passed in 0.03s =================</span>
```
</div>