6.5 KiB
📨 💪
🕐❔ 👆 💪 📨 📊 ⚪️➡️ 👩💻 (➡️ 💬, 🖥) 👆 🛠️, 👆 📨 ⚫️ 📨 💪.
📨 💪 📊 📨 👩💻 👆 🛠️. 📨 💪 💽 👆 🛠️ 📨 👩💻.
👆 🛠️ 🌖 🕧 ✔️ 📨 📨 💪. ✋️ 👩💻 🚫 🎯 💪 📨 📨 💪 🌐 🕰.
📣 📨 💪, 👆 ⚙️ Pydantic 🏷 ⏮️ 🌐 👫 🏋️ & 💰.
!!! info
📨 💽, 👆 🔜 ⚙️ 1️⃣: POST
(🌅 ⚠), PUT
, DELETE
⚖️ PATCH
.
📨 💪 ⏮️ `GET` 📨 ✔️ ⚠ 🎭 🔧, 👐, ⚫️ 🐕🦺 FastAPI, 🕴 📶 🏗/😕 ⚙️ 💼.
⚫️ 🚫, 🎓 🩺 ⏮️ 🦁 🎚 🏆 🚫 🎦 🧾 💪 🕐❔ ⚙️ `GET`, & 🗳 🖕 💪 🚫 🐕🦺 ⚫️.
🗄 Pydantic BaseModel
🥇, 👆 💪 🗄 BaseModel
⚪️➡️ pydantic
:
=== "🐍 3️⃣.6️⃣ & 🔛"
```Python hl_lines="4"
{!> ../../../docs_src/body/tutorial001.py!}
```
=== "🐍 3️⃣.1️⃣0️⃣ & 🔛"
```Python hl_lines="2"
{!> ../../../docs_src/body/tutorial001_py310.py!}
```
✍ 👆 💽 🏷
⤴️ 👆 📣 👆 💽 🏷 🎓 👈 😖 ⚪️➡️ BaseModel
.
⚙️ 🐩 🐍 🆎 🌐 🔢:
=== "🐍 3️⃣.6️⃣ & 🔛"
```Python hl_lines="7-11"
{!> ../../../docs_src/body/tutorial001.py!}
```
=== "🐍 3️⃣.1️⃣0️⃣ & 🔛"
```Python hl_lines="5-9"
{!> ../../../docs_src/body/tutorial001_py310.py!}
```
🎏 🕐❔ 📣 🔢 🔢, 🕐❔ 🏷 🔢 ✔️ 🔢 💲, ⚫️ 🚫 ✔. ⏪, ⚫️ ✔. ⚙️ None
⚒ ⚫️ 📦.
🖼, 👉 🏷 🔛 📣 🎻 "object
" (⚖️ 🐍 dict
) 💖:
{
"name": "Foo",
"description": "An optional description",
"price": 45.2,
"tax": 3.5
}
... description
& tax
📦 (⏮️ 🔢 💲 None
), 👉 🎻 "object
" 🔜 ☑:
{
"name": "Foo",
"price": 45.2
}
📣 ⚫️ 🔢
🚮 ⚫️ 👆 ➡ 🛠️, 📣 ⚫️ 🎏 🌌 👆 📣 ➡ & 🔢 🔢:
=== "🐍 3️⃣.6️⃣ & 🔛"
```Python hl_lines="18"
{!> ../../../docs_src/body/tutorial001.py!}
```
=== "🐍 3️⃣.1️⃣0️⃣ & 🔛"
```Python hl_lines="16"
{!> ../../../docs_src/body/tutorial001_py310.py!}
```
...& 📣 🚮 🆎 🏷 👆 ✍, Item
.
🏁
⏮️ 👈 🐍 🆎 📄, FastAPI 🔜:
- ✍ 💪 📨 🎻.
- 🗜 🔗 🆎 (🚥 💪).
- ✔ 💽.
- 🚥 💽 ❌, ⚫️ 🔜 📨 👌 & 🆑 ❌, ☠️ ⚫️❔ 🌐❔ & ⚫️❔ ❌ 📊.
- 🤝 👆 📨 📊 🔢
item
.- 👆 📣 ⚫️ 🔢 🆎
Item
, 👆 🔜 ✔️ 🌐 👨🎨 🐕🦺 (🛠️, ♒️) 🌐 🔢 & 👫 🆎.
- 👆 📣 ⚫️ 🔢 🆎
- 🏗 🎻 🔗 🔑 👆 🏷, 👆 💪 ⚙️ 👫 🙆 🙆 👆 💖 🚥 ⚫️ ⚒ 🔑 👆 🏗.
- 👈 🔗 🔜 🍕 🏗 🗄 🔗, & ⚙️ 🏧 🧾 ⚜.
🏧 🩺
🎻 🔗 👆 🏷 🔜 🍕 👆 🗄 🏗 🔗, & 🔜 🎦 🎓 🛠️ 🩺:

& 🔜 ⚙️ 🛠️ 🩺 🔘 🔠 ➡ 🛠️ 👈 💪 👫:

👨🎨 🐕🦺
👆 👨🎨, 🔘 👆 🔢 👆 🔜 🤚 🆎 🔑 & 🛠️ 🌐 (👉 🚫🔜 🔨 🚥 👆 📨 dict
↩️ Pydantic 🏷):

👆 🤚 ❌ ✅ ❌ 🆎 🛠️:

👉 🚫 🤞, 🎂 🛠️ 🏗 🤭 👈 🔧.
& ⚫️ 🙇 💯 🔧 🌓, ⏭ 🙆 🛠️, 🚚 ⚫️ 🔜 👷 ⏮️ 🌐 👨🎨.
📤 🔀 Pydantic ⚫️ 🐕🦺 👉.
⏮️ 🖼 ✊ ⏮️ 🎙 🎙 📟.
✋️ 👆 🔜 🤚 🎏 👨🎨 🐕🦺 ⏮️ 🗒 & 🌅 🎏 🐍 👨🎨:

!!! tip 🚥 👆 ⚙️ 🗒 👆 👨🎨, 👆 💪 ⚙️ Pydantic 🗒 📁.
⚫️ 📉 👨🎨 🐕🦺 Pydantic 🏷, ⏮️:
* 🚘-🛠️
* 🆎 ✅
* 🛠️
* 🔎
* 🔬
⚙️ 🏷
🔘 🔢, 👆 💪 🔐 🌐 🔢 🏷 🎚 🔗:
=== "🐍 3️⃣.6️⃣ & 🔛"
```Python hl_lines="21"
{!> ../../../docs_src/body/tutorial002.py!}
```
=== "🐍 3️⃣.1️⃣0️⃣ & 🔛"
```Python hl_lines="19"
{!> ../../../docs_src/body/tutorial002_py310.py!}
```
📨 💪 ➕ ➡ 🔢
👆 💪 📣 ➡ 🔢 & 📨 💪 🎏 🕰.
FastAPI 🔜 🤔 👈 🔢 🔢 👈 🏏 ➡ 🔢 🔜 ✊ ⚪️➡️ ➡, & 👈 🔢 🔢 👈 📣 Pydantic 🏷 🔜 ✊ ⚪️➡️ 📨 💪.
=== "🐍 3️⃣.6️⃣ & 🔛"
```Python hl_lines="17-18"
{!> ../../../docs_src/body/tutorial003.py!}
```
=== "🐍 3️⃣.1️⃣0️⃣ & 🔛"
```Python hl_lines="15-16"
{!> ../../../docs_src/body/tutorial003_py310.py!}
```
📨 💪 ➕ ➡ ➕ 🔢 🔢
👆 💪 📣 💪, ➡ & 🔢 🔢, 🌐 🎏 🕰.
FastAPI 🔜 🤔 🔠 👫 & ✊ 📊 ⚪️➡️ ☑ 🥉.
=== "🐍 3️⃣.6️⃣ & 🔛"
```Python hl_lines="18"
{!> ../../../docs_src/body/tutorial004.py!}
```
=== "🐍 3️⃣.1️⃣0️⃣ & 🔛"
```Python hl_lines="16"
{!> ../../../docs_src/body/tutorial004_py310.py!}
```
🔢 🔢 🔜 🤔 ⏩:
- 🚥 🔢 📣 ➡, ⚫️ 🔜 ⚙️ ➡ 🔢.
- 🚥 🔢 ⭐ 🆎 (💖
int
,float
,str
,bool
, ♒️) ⚫️ 🔜 🔬 🔢 🔢. - 🚥 🔢 📣 🆎 Pydantic 🏷, ⚫️ 🔜 🔬 📨 💪.
!!! note
FastAPI 🔜 💭 👈 💲 q
🚫 ✔ ↩️ 🔢 💲 = None
.
`Union` `Union[str, None]` 🚫 ⚙️ FastAPI, ✋️ 🔜 ✔ 👆 👨🎨 🤝 👆 👍 🐕🦺 & 🔍 ❌.
🍵 Pydantic
🚥 👆 🚫 💚 ⚙️ Pydantic 🏷, 👆 💪 ⚙️ 💪 🔢. 👀 🩺 💪 - 💗 🔢: ⭐ 💲 💪{.internal-link target=_blank}.