4.0 KiB
🔁 🗄 (🔗) 💽
👆 💪 ⚙️ encode/databases
⏮️ FastAPI 🔗 💽 ⚙️ async
& await
.
⚫️ 🔗 ⏮️:
- ✳
- ✳
- 🗄
👉 🖼, 👥 🔜 ⚙️ 🗄, ↩️ ⚫️ ⚙️ 👁 📁 & 🐍 ✔️ 🛠️ 🐕🦺. , 👆 💪 📁 👉 🖼 & 🏃 ⚫️.
⏪, 👆 🏭 🈸, 👆 💪 💚 ⚙️ 💽 💽 💖 ✳.
!!! tip 👆 💪 🛠️ 💭 ⚪️➡️ 📄 🔃 🇸🇲 🐜 (🗄 (🔗) 💽{.internal-link target=_blank}), 💖 ⚙️ 🚙 🔢 🎭 🛠️ 💽, 🔬 👆 FastAPI 📟.
👉 📄 🚫 ✔ 📚 💭, 🌓 😑 <a href="https://www.starlette.io/database/" class="external-link" target="_blank">💃</a>.
🗄 & ⚒ 🆙 SQLAlchemy
- 🗄
SQLAlchemy
. - ✍
metadata
🎚. - ✍ 🏓
notes
⚙️metadata
🎚.
{!../../../docs_src/async_sql_databases/tutorial001.py!}
!!! tip 👀 👈 🌐 👉 📟 😁 🇸🇲 🐚.
`databases` 🚫 🔨 🕳 📥.
🗄 & ⚒ 🆙 databases
- 🗄
databases
. - ✍
DATABASE_URL
. - ✍
database
🎚.
{!../../../docs_src/async_sql_databases/tutorial001.py!}
!!! tip
🚥 👆 🔗 🎏 💽 (✅ ✳), 👆 🔜 💪 🔀 DATABASE_URL
.
✍ 🏓
👉 💼, 👥 🏗 🏓 🎏 🐍 📁, ✋️ 🏭, 👆 🔜 🎲 💚 ✍ 👫 ⏮️ ⚗, 🛠️ ⏮️ 🛠️, ♒️.
📥, 👉 📄 🔜 🏃 🔗, ▶️️ ⏭ ▶️ 👆 FastAPI 🈸.
- ✍
engine
. - ✍ 🌐 🏓 ⚪️➡️
metadata
🎚.
{!../../../docs_src/async_sql_databases/tutorial001.py!}
✍ 🏷
✍ Pydantic 🏷:
- 🗒 ✍ (
NoteIn
). - 🗒 📨 (
Note
).
{!../../../docs_src/async_sql_databases/tutorial001.py!}
🏗 👫 Pydantic 🏷, 🔢 💽 🔜 ✔, 🎻 (🗜), & ✍ (📄).
, 👆 🔜 💪 👀 ⚫️ 🌐 🎓 🛠️ 🩺.
🔗 & 🔌
- ✍ 👆
FastAPI
🈸. - ✍ 🎉 🐕🦺 🔗 & 🔌 ⚪️➡️ 💽.
{!../../../docs_src/async_sql_databases/tutorial001.py!}
✍ 🗒
✍ ➡ 🛠️ 🔢 ✍ 🗒:
{!../../../docs_src/async_sql_databases/tutorial001.py!}
!!! Note
👀 👈 👥 🔗 ⏮️ 💽 ⚙️ await
, ➡ 🛠️ 🔢 📣 ⏮️ async
.
👀 response_model=List[Note]
⚫️ ⚙️ typing.List
.
👈 📄 (& ✔, 🎻, ⛽) 🔢 💽, list
Note
Ⓜ.
✍ 🗒
✍ ➡ 🛠️ 🔢 ✍ 🗒:
{!../../../docs_src/async_sql_databases/tutorial001.py!}
!!! Note
👀 👈 👥 🔗 ⏮️ 💽 ⚙️ await
, ➡ 🛠️ 🔢 📣 ⏮️ async
.
🔃 {**note.dict(), "id": last_record_id}
note
Pydantic Note
🎚.
note.dict()
📨 dict
⏮️ 🚮 💽, 🕳 💖:
{
"text": "Some note",
"completed": False,
}
✋️ ⚫️ 🚫 ✔️ id
🏑.
👥 ✍ 🆕 dict
, 👈 🔌 🔑-💲 👫 ⚪️➡️ note.dict()
⏮️:
{**note.dict()}
**note.dict()
"unpacks" the key value pairs directly, so, {**note.dict()}
would be, more or less, a copy of note.dict()
.
& ⤴️, 👥 ↔ 👈 📁 dict
, ❎ ➕1️⃣ 🔑-💲 👫: "id": last_record_id
:
{**note.dict(), "id": last_record_id}
, 🏁 🏁 📨 🔜 🕳 💖:
{
"id": 1,
"text": "Some note",
"completed": False,
}
✅ ⚫️
👆 💪 📁 👉 📟, & 👀 🩺 http://127.0.0.1:8000/docs.
📤 👆 💪 👀 🌐 👆 🛠️ 📄 & 🔗 ⏮️ ⚫️:

🌅 ℹ
👆 💪 ✍ 🌅 🔃 encode/databases
🚮 📂 📃.