📨 💪¶
🕐❔ 👆 💪 📨 📊 ⚪️➡️ 👩💻 (➡️ 💬, 🖥) 👆 🛠️, 👆 📨 ⚫️ 📨 💪.
📨 💪 📊 📨 👩💻 👆 🛠️. 📨 💪 💽 👆 🛠️ 📨 👩💻.
👆 🛠️ 🌖 🕧 ✔️ 📨 📨 💪. ✋️ 👩💻 🚫 🎯 💪 📨 📨 💪 🌐 🕰.
📣 📨 💪, 👆 ⚙️ Pydantic 🏷 ⏮️ 🌐 👫 🏋️ & 💰.
Info
📨 💽, 👆 🔜 ⚙️ 1️⃣: POST
(🌅 ⚠), PUT
, DELETE
⚖️ PATCH
.
📨 💪 ⏮️ GET
📨 ✔️ ⚠ 🎭 🔧, 👐, ⚫️ 🐕🦺 FastAPI, 🕴 📶 🏗/😕 ⚙️ 💼.
⚫️ 🚫, 🎓 🩺 ⏮️ 🦁 🎚 🏆 🚫 🎦 🧾 💪 🕐❔ ⚙️ GET
, & 🗳 🖕 💪 🚫 🐕🦺 ⚫️.
🗄 Pydantic BaseModel
¶
🥇, 👆 💪 🗄 BaseModel
⚪️➡️ pydantic
:
from typing import Union
from fastapi import FastAPI
from pydantic import BaseModel
class Item(BaseModel):
name: str
description: Union[str, None] = None
price: float
tax: Union[float, None] = None
app = FastAPI()
@app.post("/items/")
async def create_item(item: Item):
return item
from fastapi import FastAPI
from pydantic import BaseModel
class Item(BaseModel):
name: str
description: str | None = None
price: float
tax: float | None = None
app = FastAPI()
@app.post("/items/")
async def create_item(item: Item):
return item
✍ 👆 💽 🏷¶
⤴️ 👆 📣 👆 💽 🏷 🎓 👈 😖 ⚪️➡️ BaseModel
.
⚙️ 🐩 🐍 🆎 🌐 🔢:
from typing import Union
from fastapi import FastAPI
from pydantic import BaseModel
class Item(BaseModel):
name: str
description: Union[str, None] = None
price: float
tax: Union[float, None] = None
app = FastAPI()
@app.post("/items/")
async def create_item(item: Item):
return item
from fastapi import FastAPI
from pydantic import BaseModel
class Item(BaseModel):
name: str
description: str | None = None
price: float
tax: float | None = None
app = FastAPI()
@app.post("/items/")
async def create_item(item: Item):
return item
🎏 🕐❔ 📣 🔢 🔢, 🕐❔ 🏷 🔢 ✔️ 🔢 💲, ⚫️ 🚫 ✔. ⏪, ⚫️ ✔. ⚙️ None
⚒ ⚫️ 📦.
🖼, 👉 🏷 🔛 📣 🎻 "object
" (⚖️ 🐍 dict
) 💖:
{
"name": "Foo",
"description": "An optional description",
"price": 45.2,
"tax": 3.5
}
... description
& tax
📦 (⏮️ 🔢 💲 None
), 👉 🎻 "object
" 🔜 ☑:
{
"name": "Foo",
"price": 45.2
}
📣 ⚫️ 🔢¶
🚮 ⚫️ 👆 ➡ 🛠️, 📣 ⚫️ 🎏 🌌 👆 📣 ➡ & 🔢 🔢:
from typing import Union
from fastapi import FastAPI
from pydantic import BaseModel
class Item(BaseModel):
name: str
description: Union[str, None] = None
price: float
tax: Union[float, None] = None
app = FastAPI()
@app.post("/items/")
async def create_item(item: Item):
return item
from fastapi import FastAPI
from pydantic import BaseModel
class Item(BaseModel):
name: str
description: str | None = None
price: float
tax: float | None = None
app = FastAPI()
@app.post("/items/")
async def create_item(item: Item):
return item
...& 📣 🚮 🆎 🏷 👆 ✍, Item
.
🏁¶
⏮️ 👈 🐍 🆎 📄, FastAPI 🔜:
- ✍ 💪 📨 🎻.
- 🗜 🔗 🆎 (🚥 💪).
- ✔ 💽.
- 🚥 💽 ❌, ⚫️ 🔜 📨 👌 & 🆑 ❌, ☠️ ⚫️❔ 🌐❔ & ⚫️❔ ❌ 📊.
- 🤝 👆 📨 📊 🔢
item
.- 👆 📣 ⚫️ 🔢 🆎
Item
, 👆 🔜 ✔️ 🌐 👨🎨 🐕🦺 (🛠️, ♒️) 🌐 🔢 & 👫 🆎.
- 👆 📣 ⚫️ 🔢 🆎
- 🏗 🎻 🔗 🔑 👆 🏷, 👆 💪 ⚙️ 👫 🙆 🙆 👆 💖 🚥 ⚫️ ⚒ 🔑 👆 🏗.
- 👈 🔗 🔜 🍕 🏗 🗄 🔗, & ⚙️ 🏧 🧾 ⚜.
🏧 🩺¶
🎻 🔗 👆 🏷 🔜 🍕 👆 🗄 🏗 🔗, & 🔜 🎦 🎓 🛠️ 🩺:
& 🔜 ⚙️ 🛠️ 🩺 🔘 🔠 ➡ 🛠️ 👈 💪 👫:
👨🎨 🐕🦺¶
👆 👨🎨, 🔘 👆 🔢 👆 🔜 🤚 🆎 🔑 & 🛠️ 🌐 (👉 🚫🔜 🔨 🚥 👆 📨 dict
↩️ Pydantic 🏷):
👆 🤚 ❌ ✅ ❌ 🆎 🛠️:
👉 🚫 🤞, 🎂 🛠️ 🏗 🤭 👈 🔧.
& ⚫️ 🙇 💯 🔧 🌓, ⏭ 🙆 🛠️, 🚚 ⚫️ 🔜 👷 ⏮️ 🌐 👨🎨.
📤 🔀 Pydantic ⚫️ 🐕🦺 👉.
⏮️ 🖼 ✊ ⏮️ 🎙 🎙 📟.
✋️ 👆 🔜 🤚 🎏 👨🎨 🐕🦺 ⏮️ 🗒 & 🌅 🎏 🐍 👨🎨:
⚙️ 🏷¶
🔘 🔢, 👆 💪 🔐 🌐 🔢 🏷 🎚 🔗:
from typing import Union
from fastapi import FastAPI
from pydantic import BaseModel
class Item(BaseModel):
name: str
description: Union[str, None] = None
price: float
tax: Union[float, None] = None
app = FastAPI()
@app.post("/items/")
async def create_item(item: Item):
item_dict = item.dict()
if item.tax:
price_with_tax = item.price + item.tax
item_dict.update({"price_with_tax": price_with_tax})
return item_dict
from fastapi import FastAPI
from pydantic import BaseModel
class Item(BaseModel):
name: str
description: str | None = None
price: float
tax: float | None = None
app = FastAPI()
@app.post("/items/")
async def create_item(item: Item):
item_dict = item.dict()
if item.tax:
price_with_tax = item.price + item.tax
item_dict.update({"price_with_tax": price_with_tax})
return item_dict
📨 💪 ➕ ➡ 🔢¶
👆 💪 📣 ➡ 🔢 & 📨 💪 🎏 🕰.
FastAPI 🔜 🤔 👈 🔢 🔢 👈 🏏 ➡ 🔢 🔜 ✊ ⚪️➡️ ➡, & 👈 🔢 🔢 👈 📣 Pydantic 🏷 🔜 ✊ ⚪️➡️ 📨 💪.
from typing import Union
from fastapi import FastAPI
from pydantic import BaseModel
class Item(BaseModel):
name: str
description: Union[str, None] = None
price: float
tax: Union[float, None] = None
app = FastAPI()
@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item):
return {"item_id": item_id, **item.dict()}
from fastapi import FastAPI
from pydantic import BaseModel
class Item(BaseModel):
name: str
description: str | None = None
price: float
tax: float | None = None
app = FastAPI()
@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item):
return {"item_id": item_id, **item.dict()}
📨 💪 ➕ ➡ ➕ 🔢 🔢¶
👆 💪 📣 💪, ➡ & 🔢 🔢, 🌐 🎏 🕰.
FastAPI 🔜 🤔 🔠 👫 & ✊ 📊 ⚪️➡️ ☑ 🥉.
from typing import Union
from fastapi import FastAPI
from pydantic import BaseModel
class Item(BaseModel):
name: str
description: Union[str, None] = None
price: float
tax: Union[float, None] = None
app = FastAPI()
@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item, q: Union[str, None] = None):
result = {"item_id": item_id, **item.dict()}
if q:
result.update({"q": q})
return result
from fastapi import FastAPI
from pydantic import BaseModel
class Item(BaseModel):
name: str
description: str | None = None
price: float
tax: float | None = None
app = FastAPI()
@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item, q: str | None = None):
result = {"item_id": item_id, **item.dict()}
if q:
result.update({"q": q})
return result
🔢 🔢 🔜 🤔 ⏩:
- 🚥 🔢 📣 ➡, ⚫️ 🔜 ⚙️ ➡ 🔢.
- 🚥 🔢 ⭐ 🆎 (💖
int
,float
,str
,bool
, ♒️) ⚫️ 🔜 🔬 🔢 🔢. - 🚥 🔢 📣 🆎 Pydantic 🏷, ⚫️ 🔜 🔬 📨 💪.
Note
FastAPI 🔜 💭 👈 💲 q
🚫 ✔ ↩️ 🔢 💲 = None
.
Union
Union[str, None]
🚫 ⚙️ FastAPI, ✋️ 🔜 ✔ 👆 👨🎨 🤝 👆 👍 🐕🦺 & 🔍 ❌.
🍵 Pydantic¶
🚥 👆 🚫 💚 ⚙️ Pydantic 🏷, 👆 💪 ⚙️ 💪 🔢. 👀 🩺 💪 - 💗 🔢: ⭐ 💲 💪.