🎻 🔗 🔢¶
📤 💼 🌐❔ 👆 5️⃣📆 💪 🗜 💽 🆎 (💖 Pydantic 🏷) 🕳 🔗 ⏮️ 🎻 (💖 dict
, list
, ♒️).
🖼, 🚥 👆 💪 🏪 ⚫️ 💽.
👈, FastAPI 🚚 jsonable_encoder()
🔢.
⚙️ jsonable_encoder
¶
➡️ 🌈 👈 👆 ✔️ 💽 fake_db
👈 🕴 📨 🎻 🔗 💽.
🖼, ⚫️ 🚫 📨 datetime
🎚, 👈 🚫 🔗 ⏮️ 🎻.
, datetime
🎚 🔜 ✔️ 🗜 str
⚗ 💽 💾 📁.
🎏 🌌, 👉 💽 🚫🔜 📨 Pydantic 🏷 (🎚 ⏮️ 🔢), 🕴 dict
.
👆 💪 ⚙️ jsonable_encoder
👈.
⚫️ 📨 🎚, 💖 Pydantic 🏷, & 📨 🎻 🔗 ⏬:
from datetime import datetime
from typing import Union
from fastapi import FastAPI
from fastapi.encoders import jsonable_encoder
from pydantic import BaseModel
fake_db = {}
class Item(BaseModel):
title: str
timestamp: datetime
description: Union[str, None] = None
app = FastAPI()
@app.put("/items/{id}")
def update_item(id: str, item: Item):
json_compatible_item_data = jsonable_encoder(item)
fake_db[id] = json_compatible_item_data
🤓 Other versions and variants
from datetime import datetime
from fastapi import FastAPI
from fastapi.encoders import jsonable_encoder
from pydantic import BaseModel
fake_db = {}
class Item(BaseModel):
title: str
timestamp: datetime
description: str | None = None
app = FastAPI()
@app.put("/items/{id}")
def update_item(id: str, item: Item):
json_compatible_item_data = jsonable_encoder(item)
fake_db[id] = json_compatible_item_data
👉 🖼, ⚫️ 🔜 🗜 Pydantic 🏷 dict
, & datetime
str
.
🏁 🤙 ⚫️ 🕳 👈 💪 🗜 ⏮️ 🐍 🐩 json.dumps()
.
⚫️ 🚫 📨 ⭕ str
⚗ 💽 🎻 📁 (🎻). ⚫️ 📨 🐍 🐩 💽 📊 (✅ dict
) ⏮️ 💲 & 🎧-💲 👈 🌐 🔗 ⏮️ 🎻.
Note
jsonable_encoder
🤙 ⚙️ FastAPI 🔘 🗜 💽. ✋️ ⚫️ ⚠ 📚 🎏 😐.