Skip to content

๐Ÿ“จ ๐Ÿ’ช

๐Ÿ•โ” ๐Ÿ‘† ๐Ÿ’ช ๐Ÿ“จ ๐Ÿ“Š โšช๏ธโžก๏ธ ๐Ÿ‘ฉโ€๐Ÿ’ป (โžก๏ธ ๐Ÿ’ฌ, ๐Ÿ–ฅ) ๐Ÿ‘† ๐Ÿ› ๏ธ, ๐Ÿ‘† ๐Ÿ“จ โšซ๏ธ ๐Ÿ“จ ๐Ÿ’ช.

๐Ÿ“จ ๐Ÿ’ช ๐Ÿ“Š ๐Ÿ“จ ๐Ÿ‘ฉโ€๐Ÿ’ป ๐Ÿ‘† ๐Ÿ› ๏ธ. ๐Ÿ“จ ๐Ÿ’ช ๐Ÿ’ฝ ๐Ÿ‘† ๐Ÿ› ๏ธ ๐Ÿ“จ ๐Ÿ‘ฉโ€๐Ÿ’ป.

๐Ÿ‘† ๐Ÿ› ๏ธ ๐ŸŒ– ๐Ÿ•ง โœ”๏ธ ๐Ÿ“จ ๐Ÿ“จ ๐Ÿ’ช. โœ‹๏ธ ๐Ÿ‘ฉโ€๐Ÿ’ป ๐Ÿšซ ๐ŸŽฏ ๐Ÿ’ช ๐Ÿ“จ ๐Ÿ“จ ๐Ÿ’ช ๐ŸŒ ๐Ÿ•ฐ.

๐Ÿ“ฃ ๐Ÿ“จ ๐Ÿ’ช, ๐Ÿ‘† โš™๏ธ 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 โšซ๏ธ ๐Ÿ•โ€๐Ÿฆบ ๐Ÿ‘‰.

โฎ๏ธ ๐Ÿ–ผ โœŠ โฎ๏ธ ๐ŸŽ™ ๐ŸŽ™ ๐Ÿ“Ÿ.

โœ‹๏ธ ๐Ÿ‘† ๐Ÿ”œ ๐Ÿคš ๐ŸŽ ๐Ÿ‘จโ€๐ŸŽจ ๐Ÿ•โ€๐Ÿฆบ โฎ๏ธ ๐Ÿ—’ & ๐ŸŒ… ๐ŸŽ ๐Ÿ ๐Ÿ‘จโ€๐ŸŽจ:

Tip

๐Ÿšฅ ๐Ÿ‘† โš™๏ธ ๐Ÿ—’ ๐Ÿ‘† ๐Ÿ‘จโ€๐ŸŽจ, ๐Ÿ‘† ๐Ÿ’ช โš™๏ธ 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 create_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 create_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 create_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 create_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 ๐Ÿท, ๐Ÿ‘† ๐Ÿ’ช โš™๏ธ ๐Ÿ’ช ๐Ÿ”ข. ๐Ÿ‘€ ๐Ÿฉบ ๐Ÿ’ช - ๐Ÿ’— ๐Ÿ”ข: โญ ๐Ÿ’ฒ ๐Ÿ’ช.