fastapi/docs/em/docs/tutorial/extra-models.md

253 lines
6.8 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 🏷
▶️ ⏮️ ⏮️ 🖼, ⚫️ 🔜 ⚠ ✔️ 🌅 🌘 1⃣ 🔗 🏷.
👉 ✴️ 💼 👩‍💻 🏷, ↩️:
* **🔢 🏷** 💪 💪 ✔️ 🔐.
* **🔢 🏷** 🔜 🚫 ✔️ 🔐.
* **💽 🏷** 🔜 🎲 💪 ✔️ #⃣ 🔐.
!!! danger
🙅 🏪 👩‍💻 🔢 🔐. 🕧 🏪 "🔐 #⃣" 👈 👆 💪 ⤴️ ✔.
🚥 👆 🚫 💭, 👆 🔜 💡 ⚫️❔ "🔐#️⃣" [💂‍♂ 📃](security/simple-oauth2.md#_4){.internal-link target=_blank}.
## 💗 🏷
📥 🏢 💭 ❔ 🏷 💪 👀 💖 ⏮️ 👫 🔐 🏑 & 🥉 🌐❔ 👫 ⚙️:
=== "🐍 3⃣.6️⃣ & 🔛"
```Python hl_lines="9 11 16 22 24 29-30 33-35 40-41"
{!> ../../../docs_src/extra_models/tutorial001.py!}
```
=== "🐍 3⃣.1⃣0& 🔛"
```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")
```
& ⤴️ 👥 🤙:
```Python
user_dict = user_in.dict()
```
👥 🔜 ✔️ `dict` ⏮️ 💽 🔢 `user_dict` (⚫️ `dict` ↩️ Pydantic 🏷 🎚).
& 🚥 👥 🤙:
```Python
print(user_dict)
```
👥 🔜 🤚 🐍 `dict` ⏮️:
```Python
{
'username': 'john',
'password': 'secret',
'email': 'john.doe@example.com',
'full_name': None,
}
```
#### 🎁 `dict`
🚥 👥 ✊ `dict` 💖 `user_dict` & 🚶‍♀️ ⚫️ 🔢 (⚖️ 🎓) ⏮️ `**user_dict`, 🐍 🔜 "🎁" ⚫️. ⚫️ 🔜 🚶‍♀️ 🔑 & 💲 `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`, & ⤴️ 👥 ⚒ 🐍 "🎁" ⚫️ 🚶‍♀️ ⚫️ `UserInDB` 🔠 ⏮️ `**`.
, 👥 🤚 Pydantic 🏷 ⚪️➡️ 💽 1⃣ Pydantic 🏷.
#### 🎁 `dict` & 🇨🇻
& ⤴️ ❎ 🇨🇻 ❌ `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⃣ 🥉 ✋️ 🚫 🎏), ♒️.
& 👉 🏷 🌐 🤝 📚 💽 & ❎ 🔢 📛 & 🆎.
👥 💪 👻.
👥 💪 📣 `UserBase` 🏷 👈 🍦 🧢 👆 🎏 🏷. & ⤴️ 👥 💪 ⚒ 🏿 👈 🏷 👈 😖 🚮 🔢 (🆎 📄, 🔬, ♒️).
🌐 💽 🛠️, 🔬, 🧾, ♒️. 🔜 👷 🛎.
👈 🌌, 👥 💪 📣 🔺 🖖 🏷 (⏮️ 🔢 `password`, ⏮️ `hashed_password` & 🍵 🔐):
=== "🐍 3⃣.6️⃣ & 🔛"
```Python hl_lines="9 15-16 19-20 23-24"
{!> ../../../docs_src/extra_models/tutorial002.py!}
```
=== "🐍 3⃣.1⃣0& 🔛"
```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://docs.pydantic.dev/latest/concepts/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; 🙅‍♂ 🔐.