6.8 KiB
➕ 🏷
▶️ ⏮️ ⏮️ 🖼, ⚫️ 🔜 ⚠ ✔️ 🌅 🌘 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
💖:
user_in = UserIn(username="john", password="secret", email="[email protected]")
& ⤴️ 👥 🤙:
user_dict = user_in.dict()
👥 🔜 ✔️ dict
⏮️ 💽 🔢 user_dict
(⚫️ dict
↩️ Pydantic 🏷 🎚).
& 🚥 👥 🤙:
print(user_dict)
👥 🔜 🤚 🐍 dict
⏮️:
{
'username': 'john',
'password': 'secret',
'email': '[email protected]',
'full_name': None,
}
🎁 dict
🚥 👥 ✊ dict
💖 user_dict
& 🚶♀️ ⚫️ 🔢 (⚖️ 🎓) ⏮️ **user_dict
, 🐍 🔜 "🎁" ⚫️. ⚫️ 🔜 🚶♀️ 🔑 & 💲 user_dict
🔗 🔑-💲 ❌.
, ▶️ ⏮️ user_dict
⚪️➡️ 🔛, ✍:
UserInDB(**user_dict)
🔜 🏁 🕳 🌓:
UserInDB(
username="john",
password="secret",
email="[email protected]",
full_name=None,
)
⚖️ 🌅 ⚫️❔, ⚙️ user_dict
🔗, ⏮️ ⚫️❔ 🎚 ⚫️ 💪 ✔️ 🔮:
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()
, 👉 📟:
user_dict = user_in.dict()
UserInDB(**user_dict)
🔜 🌓:
UserInDB(**user_in.dict())
...↩️ user_in.dict()
dict
, & ⤴️ 👥 ⚒ 🐍 "🎁" ⚫️ 🚶♀️ ⚫️ UserInDB
🔠 ⏮️ **
.
, 👥 🤚 Pydantic 🏷 ⚪️➡️ 💽 ➕1️⃣ Pydantic 🏷.
🎁 dict
& ➕ 🇨🇻
& ⤴️ ❎ ➕ 🇨🇻 ❌ hashed_password=hashed_password
, 💖:
UserInDB(**user_in.dict(), hashed_password=hashed_password)
...🔚 🆙 💆♂ 💖:
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
.
👈, ⚙️ 🐩 🐍 🆎 🔑 typing.Union
:
!!! note
🕐❔ ⚖ Union
, 🔌 🏆 🎯 🆎 🥇, ⏩ 🌘 🎯 🆎. 🖼 🔛, 🌖 🎯 PlaneItem
👟 ⏭ CarItem
Union[PlaneItem, CarItem]
.
=== "🐍 3️⃣.6️⃣ & 🔛"
```Python hl_lines="1 14-15 18-20 33"
{!> ../../../docs_src/extra_models/tutorial003.py!}
```
=== "🐍 3️⃣.1️⃣0️⃣ & 🔛"
```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️⃣.
🚥 ⚫️ 🆎 ✍ 👥 💪 ✔️ ⚙️ ⏸ ⏸,:
some_variable: PlaneItem | CarItem
✋️ 🚥 👥 🚮 👈 response_model=PlaneItem | CarItem
👥 🔜 🤚 ❌, ↩️ 🐍 🔜 🔄 🎭 ❌ 🛠️ 🖖 PlaneItem
& CarItem
↩️ 🔬 👈 🆎 ✍.
📇 🏷
🎏 🌌, 👆 💪 📣 📨 📇 🎚.
👈, ⚙️ 🐩 🐍 typing.List
(⚖️ list
🐍 3️⃣.9️⃣ & 🔛):
=== "🐍 3️⃣.6️⃣ & 🔛"
```Python hl_lines="1 20"
{!> ../../../docs_src/extra_models/tutorial004.py!}
```
=== "🐍 3️⃣.9️⃣ & 🔛"
```Python hl_lines="18"
{!> ../../../docs_src/extra_models/tutorial004_py39.py!}
```
📨 ⏮️ ❌ dict
👆 💪 📣 📨 ⚙️ ✅ ❌ dict
, 📣 🆎 🔑 & 💲, 🍵 ⚙️ Pydantic 🏷.
👉 ⚠ 🚥 👆 🚫 💭 ☑ 🏑/🔢 📛 (👈 🔜 💪 Pydantic 🏷) ⏪.
👉 💼, 👆 💪 ⚙️ typing.Dict
(⚖️ dict
🐍 3️⃣.9️⃣ & 🔛):
=== "🐍 3️⃣.6️⃣ & 🔛"
```Python hl_lines="1 8"
{!> ../../../docs_src/extra_models/tutorial005.py!}
```
=== "🐍 3️⃣.9️⃣ & 🔛"
```Python hl_lines="6"
{!> ../../../docs_src/extra_models/tutorial005_py39.py!}
```
🌃
⚙️ 💗 Pydantic 🏷 & 😖 ➡ 🔠 💼.
👆 🚫 💪 ✔️ 👁 💽 🏷 📍 👨💼 🚥 👈 👨💼 🔜 💪 ✔️ 🎏 "🇵🇸". 💼 ⏮️ 👩💻 "👨💼" ⏮️ 🇵🇸 ✅ password
, password_hash
& 🙅♂ 🔐.