๐ช - ๐¶
๐ ๐ ๐ ๐ช ๐ฃ ๐ ๐ฌ & ๐ โก ๐ ๏ธ ๐ข ๐ข โฎ๏ธ 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 ๐ฃ โ ๐ฌ & ๐ ๐ท ๐ข.
๐ ๐ช โ๏ธ โ ๐จ๐ป โ ๐ถโโ๏ธ ๐ ๐ป ๐ ๐.