5.7 KiB
➕ 🏷
▶️ ⏮️ ⏮️ 🖼, ⚫️ 🔜 ⚠ ✔️ 🌅 🌘 1️⃣ 🔗 🏷.
👉 ✴️ 💼 👩💻 🏷, ↩️:
- 🔢 🏷 💪 💪 ✔️ 🔐.
- 🔢 🏷 🔜 🚫 ✔️ 🔐.
- 💽 🏷 🔜 🎲 💪 ✔️ #️⃣ 🔐.
/// danger
🙅 🏪 👩💻 🔢 🔐. 🕧 🏪 "🔐 #️⃣" 👈 👆 💪 ⤴️ ✔.
🚥 👆 🚫 💭, 👆 🔜 💡 ⚫️❔ "🔐#️⃣" 💂♂ 📃{.internal-link target=_blank}.
///
💗 🏷
📥 🏢 💭 ❔ 🏷 💪 👀 💖 ⏮️ 👫 🔐 🏑 & 🥉 🌐❔ 👫 ⚙️:
{* ../../docs_src/extra_models/tutorial001.py hl[9,11,16,22,24,29:30,33:35,40:41] *}
🔃 **user_in.dict()
Pydantic .dict()
user_in
Pydantic 🏷 🎓 UserIn
.
Pydantic 🏷 ✔️ .dict()
👩🔬 👈 📨 dict
⏮️ 🏷 💽.
, 🚥 👥 ✍ Pydantic 🎚 user_in
💖:
user_in = UserIn(username="john", password="secret", email="john.doe@example.com")
& ⤴️ 👥 🤙:
user_dict = user_in.dict()
👥 🔜 ✔️ dict
⏮️ 💽 🔢 user_dict
(⚫️ dict
↩️ Pydantic 🏷 🎚).
& 🚥 👥 🤙:
print(user_dict)
👥 🔜 🤚 🐍 dict
⏮️:
{
'username': 'john',
'password': 'secret',
'email': 'john.doe@example.com',
'full_name': None,
}
🎁 dict
🚥 👥 ✊ dict
💖 user_dict
& 🚶♀️ ⚫️ 🔢 (⚖️ 🎓) ⏮️ **user_dict
, 🐍 🔜 "🎁" ⚫️. ⚫️ 🔜 🚶♀️ 🔑 & 💲 user_dict
🔗 🔑-💲 ❌.
, ▶️ ⏮️ user_dict
⚪️➡️ 🔛, ✍:
UserInDB(**user_dict)
🔜 🏁 🕳 🌓:
UserInDB(
username="john",
password="secret",
email="john.doe@example.com",
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
& 🍵 🔐):
{* ../../docs_src/extra_models/tutorial002.py hl[9,15:16,19:20,23:24] *}
Union
⚖️ anyOf
👆 💪 📣 📨 Union
2️⃣ 🆎, 👈 ⛓, 👈 📨 🔜 🙆 2️⃣.
⚫️ 🔜 🔬 🗄 ⏮️ anyOf
.
👈, ⚙️ 🐩 🐍 🆎 🔑 typing.Union
:
/// note
🕐❔ ⚖ Union
, 🔌 🏆 🎯 🆎 🥇, ⏩ 🌘 🎯 🆎. 🖼 🔛, 🌖 🎯 PlaneItem
👟 ⏭ CarItem
Union[PlaneItem, CarItem]
.
///
{* ../../docs_src/extra_models/tutorial003.py hl[1,14:15,18:20,33] *}
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️⃣ & 🔛):
{* ../../docs_src/extra_models/tutorial004.py hl[1,20] *}
📨 ⏮️ ❌ dict
👆 💪 📣 📨 ⚙️ ✅ ❌ dict
, 📣 🆎 🔑 & 💲, 🍵 ⚙️ Pydantic 🏷.
👉 ⚠ 🚥 👆 🚫 💭 ☑ 🏑/🔢 📛 (👈 🔜 💪 Pydantic 🏷) ⏪.
👉 💼, 👆 💪 ⚙️ typing.Dict
(⚖️ dict
🐍 3️⃣.9️⃣ & 🔛):
{* ../../docs_src/extra_models/tutorial005.py hl[1,8] *}
🌃
⚙️ 💗 Pydantic 🏷 & 😖 ➡ 🔠 💼.
👆 🚫 💪 ✔️ 👁 💽 🏷 📍 👨💼 🚥 👈 👨💼 🔜 💪 ✔️ 🎏 "🇵🇸". 💼 ⏮️ 👩💻 "👨💼" ⏮️ 🇵🇸 ✅ password
, password_hash
& 🙅♂ 🔐.