โก ๐ข & ๐ข ๐ฌ¶
๐ ๐ ๐ ๐ ๐ช ๐ฃ ๐
๐ฌ & ๐ ๐ข ๐ข โฎ๏ธ Query, ๐ ๐ช ๐ฃ ๐ ๐ ๐ฌ & ๐ โก ๐ข โฎ๏ธ Path.
๐ โก¶
๐ฅ, ๐ Path โช๏ธโก๏ธ fastapi:
from typing import Union
from fastapi import FastAPI, Path, Query
app = FastAPI()
@app.get("/items/{item_id}")
async def read_items(
item_id: int = Path(title="The ID of the item to get"),
q: Union[str, None] = Query(default=None, alias="item-query"),
):
results = {"item_id": item_id}
if q:
results.update({"q": q})
return results
from fastapi import FastAPI, Path, Query
app = FastAPI()
@app.get("/items/{item_id}")
async def read_items(
item_id: int = Path(title="The ID of the item to get"),
q: str | None = Query(default=None, alias="item-query"),
):
results = {"item_id": item_id}
if q:
results.update({"q": q})
return results
๐ฃ ๐¶
๐ ๐ช ๐ฃ ๐ ๐ ๐ข Query.
๐ผ, ๐ฃ title ๐ ๐ฒ โก ๐ข item_id ๐ ๐ช ๐:
from typing import Union
from fastapi import FastAPI, Path, Query
app = FastAPI()
@app.get("/items/{item_id}")
async def read_items(
item_id: int = Path(title="The ID of the item to get"),
q: Union[str, None] = Query(default=None, alias="item-query"),
):
results = {"item_id": item_id}
if q:
results.update({"q": q})
return results
from fastapi import FastAPI, Path, Query
app = FastAPI()
@app.get("/items/{item_id}")
async def read_items(
item_id: int = Path(title="The ID of the item to get"),
q: str | None = Query(default=None, alias="item-query"),
):
results = {"item_id": item_id}
if q:
results.update({"q": q})
return results
Note
โก ๐ข ๐ง โ โซ๏ธ โ๏ธ ๐ โก.
, ๐ ๐ ๐ฃ โซ๏ธ โฎ๏ธ ... โข โซ๏ธ โ.
๐, ๐ฅ ๐ ๐ฃ โซ๏ธ โฎ๏ธ None โ๏ธ โ ๐ข ๐ฒ, โซ๏ธ ๐ ๐ซ ๐ ๐ณ, โซ๏ธ ๐ ๐ง ๐.
โ ๐ข ๐ ๐ช¶
โก๏ธ ๐ฌ ๐ ๐ ๐ ๐ฃ ๐ข ๐ข q โ str.
& ๐ ๐ซ ๐ช ๐ฃ ๐ณ ๐ ๐ ๐ข, ๐ ๐ซ ๐ค ๐ช โ๏ธ Query.
โ๏ธ ๐ ๐ช โ๏ธ Path item_id โก ๐ข.
๐ ๐ ๐ญ ๐ฅ ๐ ๐ฎ ๐ฒ โฎ๏ธ "๐ข" โญ ๐ฒ ๐ ๐ซ โ๏ธ "๐ข".
โ๏ธ ๐ ๐ช ๐ค-โ ๐ซ, & โ๏ธ ๐ฒ ๐ต ๐ข (๐ข ๐ข q) ๐ฅ.
โซ๏ธ ๐ซ ๐ค FastAPI. โซ๏ธ ๐ ๐ ๐ข ๐ซ ๐, ๐ & ๐ข ๐ (Query, Path, โ๏ธ), โซ๏ธ ๐ซ ๐
๐ โ.
, ๐ ๐ช ๐ฃ ๐ ๐ข:
from fastapi import FastAPI, Path
app = FastAPI()
@app.get("/items/{item_id}")
async def read_items(q: str, item_id: int = Path(title="The ID of the item to get")):
results = {"item_id": item_id}
if q:
results.update({"q": q})
return results
โ ๐ข ๐ ๐ช, ๐ฑ¶
๐ฅ ๐ ๐ ๐ฃ q ๐ข ๐ข ๐ต Query ๐ซ ๐ ๐ข ๐ฒ, & โก ๐ข item_id โ๏ธ Path, & โ๏ธ ๐ซ ๐ โ, ๐ โ๏ธ ๐ฅ ๐ โ ๐.
๐ถโโ๏ธ *, ๐ฅ ๐ข ๐ข.
๐ ๐ ๐ซ ๐ณ โฎ๏ธ ๐ *, โ๏ธ โซ๏ธ ๐ ๐ญ ๐ ๐ ๐ ๐ข ๐ ๐ค ๐จ๐ป โ (๐-๐ฒ ๐ซ), ๐ญ kwargs. ๐ฅ ๐ซ ๐ซ โ๏ธ ๐ข ๐ฒ.
from fastapi import FastAPI, Path
app = FastAPI()
@app.get("/items/{item_id}")
async def read_items(*, item_id: int = Path(title="The ID of the item to get"), q: str):
results = {"item_id": item_id}
if q:
results.update({"q": q})
return results
๐ข ๐ฌ: ๐ ๐ โ๏ธ ๐¶
โฎ๏ธ Query & Path (& ๐ ๐ ๐ ๐ โช) ๐ ๐ช ๐ฃ ๐ข โ.
๐ฅ, โฎ๏ธ ge=1, item_id ๐ ๐ช ๐ข ๐ข "g๐
พ ๐ โ๏ธ e๐
พ" 1.
from fastapi import FastAPI, Path
app = FastAPI()
@app.get("/items/{item_id}")
async def read_items(
*, item_id: int = Path(title="The ID of the item to get", ge=1), q: str
):
results = {"item_id": item_id}
if q:
results.update({"q": q})
return results
๐ข ๐ฌ: ๐ ๐ & ๐ ๐ โ๏ธ ๐¶
๐ โ:
gt:g๐ พt๐ฒle:l๐ญ ๐ โ๏ธe๐ พ
from fastapi import FastAPI, Path
app = FastAPI()
@app.get("/items/{item_id}")
async def read_items(
*,
item_id: int = Path(title="The ID of the item to get", gt=0, le=1000),
q: str,
):
results = {"item_id": item_id}
if q:
results.update({"q": q})
return results
๐ข ๐ฌ: ๐, ๐ ๐ & ๐ ๐¶
๐ข ๐ฌ ๐ท float ๐ฒ.
๐ฅ ๐โ โซ๏ธ โถ๏ธ๏ธ โ ๐ช ๐ฃ gt & ๐ซ ge. โฎ๏ธ โซ๏ธ ๐ ๐ช ๐, ๐ผ, ๐ ๐ฒ ๐ ๐ ๐ 0, ๐ฅ โซ๏ธ ๐ ๐ 1.
, 0.5 ๐ โ ๐ฒ. โ๏ธ 0.0 โ๏ธ 0 ๐ ๐ซ.
& ๐ lt.
from fastapi import FastAPI, Path, Query
app = FastAPI()
@app.get("/items/{item_id}")
async def read_items(
*,
item_id: int = Path(title="The ID of the item to get", ge=0, le=1000),
q: str,
size: float = Query(gt=0, lt=10.5),
):
results = {"item_id": item_id}
if q:
results.update({"q": q})
return results
๐¶
โฎ๏ธ Query, Path (& ๐ ๐ ๐ซ ๐) ๐ ๐ช ๐ฃ ๐ & ๐ป ๐ฌ ๐ ๐ โฎ๏ธ ๐ข ๐ข & ๐ป ๐ฌ.
& ๐ ๐ช ๐ฃ ๐ข ๐ฌ:
gt:g๐ พt๐ฒge:g๐ พ ๐ โ๏ธe๐ พlt:l๐ญt๐ฒle:l๐ญ ๐ โ๏ธe๐ พ
Info
Query, Path, & ๐ ๐ ๐ ๐ ๐ โช ๐ฟ โ Param ๐.
๐ ๐ซ ๐ฐ ๐ ๐ข ๐ ๐ฌ & ๐ ๐ โ๏ธ ๐.
๐ก โน
๐โ ๐ ๐ Query, Path & ๐ โช๏ธโก๏ธ fastapi, ๐ซ ๐ค ๐ข.
๐ ๐โ ๐ค, ๐จ ๐ ๐ ๐ ๐.
, ๐ ๐ Query, โ ๐ข. & ๐โ ๐ ๐ค โซ๏ธ, โซ๏ธ ๐จ ๐ ๐ ๐ Query.
๐ซ ๐ข ๐ค (โฉ๏ธ โ๏ธ ๐ ๐) ๐ ๐ ๐จโ๐จ ๐ซ โข โ ๐ ๐ซ ๐.
๐ ๐ ๐ ๐ช โ๏ธ ๐ ๐ ๐จโ๐จ & ๐ ๏ธ ๐งฐ ๐ต โ๏ธ ๐ฎ ๐ ๐ณ ๐คทโโ ๐ โ.