Skip to content

๐Ÿ’ช - ๐Ÿ‘

๐ŸŽ ๐ŸŒŒ ๐Ÿ‘† ๐Ÿ’ช ๐Ÿ“ฃ ๐ŸŒ– ๐Ÿ”ฌ & ๐Ÿ—ƒ โžก ๐Ÿ› ๏ธ ๐Ÿ”ข ๐Ÿ”ข โฎ๏ธ Query, Path & Body, ๐Ÿ‘† ๐Ÿ’ช ๐Ÿ“ฃ ๐Ÿ”ฌ & ๐Ÿ—ƒ ๐Ÿ”˜ Pydantic ๐Ÿท โš™๏ธ Pydantic Field.

๐Ÿ—„ Field

๐Ÿฅ‡, ๐Ÿ‘† โœ”๏ธ ๐Ÿ—„ โšซ๏ธ:

from typing import Union

from fastapi import Body, FastAPI
from pydantic import BaseModel, Field

app = FastAPI()


class Item(BaseModel):
    name: str
    description: Union[str, None] = Field(
        default=None, title="The description of the item", max_length=300
    )
    price: float = Field(gt=0, description="The price must be greater than zero")
    tax: Union[float, None] = None


@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item = Body(embed=True)):
    results = {"item_id": item_id, "item": item}
    return results
from fastapi import Body, FastAPI
from pydantic import BaseModel, Field

app = FastAPI()


class Item(BaseModel):
    name: str
    description: str | None = Field(
        default=None, title="The description of the item", max_length=300
    )
    price: float = Field(gt=0, description="The price must be greater than zero")
    tax: float | None = None


@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item = Body(embed=True)):
    results = {"item_id": item_id, "item": item}
    return results

Warning

๐Ÿ‘€ ๐Ÿ‘ˆ Field ๐Ÿ—„ ๐Ÿ”— โšช๏ธโžก๏ธ pydantic, ๐Ÿšซ โšช๏ธโžก๏ธ fastapi ๐ŸŒ ๐ŸŽ‚ (Query, Path, Body, โ™’๏ธ).

๐Ÿ“ฃ ๐Ÿท ๐Ÿ”ข

๐Ÿ‘† ๐Ÿ’ช โคด๏ธ โš™๏ธ Field โฎ๏ธ ๐Ÿท ๐Ÿ”ข:

from typing import Union

from fastapi import Body, FastAPI
from pydantic import BaseModel, Field

app = FastAPI()


class Item(BaseModel):
    name: str
    description: Union[str, None] = Field(
        default=None, title="The description of the item", max_length=300
    )
    price: float = Field(gt=0, description="The price must be greater than zero")
    tax: Union[float, None] = None


@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item = Body(embed=True)):
    results = {"item_id": item_id, "item": item}
    return results
from fastapi import Body, FastAPI
from pydantic import BaseModel, Field

app = FastAPI()


class Item(BaseModel):
    name: str
    description: str | None = Field(
        default=None, title="The description of the item", max_length=300
    )
    price: float = Field(gt=0, description="The price must be greater than zero")
    tax: float | None = None


@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item = Body(embed=True)):
    results = {"item_id": item_id, "item": item}
    return results

Field ๐Ÿ‘ท ๐ŸŽ ๐ŸŒŒ Query, Path & Body, โšซ๏ธ โœ”๏ธ ๐ŸŒ ๐ŸŽ ๐Ÿ”ข, โ™’๏ธ.

๐Ÿ“ก โ„น

๐Ÿค™, Query, Path & ๐ŸŽ ๐Ÿ‘† ๐Ÿ”œ ๐Ÿ‘€ โญ โœ ๐ŸŽš ๐Ÿฟ โš  Param ๐ŸŽ“, โ” โšซ๏ธ ๐Ÿฟ Pydantic FieldInfo ๐ŸŽ“.

& Pydantic Field ๐Ÿ“จ ๐Ÿ‘ FieldInfo ๐Ÿ‘.

Body ๐Ÿ“จ ๐ŸŽš ๐Ÿฟ FieldInfo ๐Ÿ”—. & ๐Ÿ“ค ๐ŸŽ ๐Ÿ‘† ๐Ÿ”œ ๐Ÿ‘€ โช ๐Ÿ‘ˆ ๐Ÿฟ Body ๐ŸŽ“.

๐Ÿ’ญ ๐Ÿ‘ˆ ๐Ÿ•โ” ๐Ÿ‘† ๐Ÿ—„ Query, Path, & ๐ŸŽ โšช๏ธโžก๏ธ fastapi, ๐Ÿ‘ˆ ๐Ÿค™ ๐Ÿ”ข ๐Ÿ‘ˆ ๐Ÿ“จ ๐ŸŽ ๐ŸŽ“.

Tip

๐Ÿ‘€ โ” ๐Ÿ”  ๐Ÿท ๐Ÿ”ข โฎ๏ธ ๐Ÿ†Ž, ๐Ÿ”ข ๐Ÿ’ฒ & Field โœ”๏ธ ๐ŸŽ ๐Ÿ“Š โžก ๐Ÿ› ๏ธ ๐Ÿ”ข ๐Ÿ”ข, โฎ๏ธ Field โ†ฉ๏ธ Path, Query & Body.

๐Ÿšฎ โž• โ„น

๐Ÿ‘† ๐Ÿ’ช ๐Ÿ“ฃ โž• โ„น Field, Query, Body, โ™’๏ธ. & โšซ๏ธ ๐Ÿ”œ ๐Ÿ”Œ ๐Ÿ— ๐ŸŽป ๐Ÿ”—.

๐Ÿ‘† ๐Ÿ”œ ๐Ÿ’ก ๐ŸŒ… ๐Ÿ”ƒ โŽ โž• โ„น โช ๐Ÿฉบ, ๐Ÿ•โ” ๐Ÿซ ๐Ÿ“ฃ ๐Ÿ–ผ.

Warning

โž• ๐Ÿ”‘ ๐Ÿšถโ€โ™€๏ธ Field ๐Ÿ”œ ๐ŸŽ ๐Ÿ“‰ ๐Ÿ—„ ๐Ÿ”— ๐Ÿ‘† ๐Ÿˆธ. ๐Ÿ‘ซ ๐Ÿ”‘ 5๏ธโƒฃ๐Ÿ“† ๐Ÿšซ ๐ŸŽฏ ๐Ÿ• ๐Ÿ—„ ๐Ÿ”ง, ๐Ÿ—„ ๐Ÿงฐ, ๐Ÿ–ผ ๐Ÿ—„ ๐Ÿ’ณ, 5๏ธโƒฃ๐Ÿ“† ๐Ÿšซ ๐Ÿ‘ท โฎ๏ธ ๐Ÿ‘† ๐Ÿ— ๐Ÿ”—.

๐ŸŒƒ

๐Ÿ‘† ๐Ÿ’ช โš™๏ธ Pydantic Field ๐Ÿ“ฃ โž• ๐Ÿ”ฌ & ๐Ÿ—ƒ ๐Ÿท ๐Ÿ”ข.

๐Ÿ‘† ๐Ÿ’ช โš™๏ธ โž• ๐Ÿ‡จ๐Ÿ‡ป โŒ ๐Ÿšถโ€โ™€๏ธ ๐ŸŒ– ๐ŸŽป ๐Ÿ”— ๐Ÿ—ƒ.