Skip to content

โžก ๐Ÿ”ข & ๐Ÿ”ข ๐Ÿ”ฌ

๐ŸŽ ๐ŸŒŒ ๐Ÿ‘ˆ ๐Ÿ‘† ๐Ÿ’ช ๐Ÿ“ฃ ๐ŸŒ… ๐Ÿ”ฌ & ๐Ÿ—ƒ ๐Ÿ”ข ๐Ÿ”ข โฎ๏ธ 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.

๐Ÿ‘ซ ๐Ÿ”ข ๐Ÿ“ค (โ†ฉ๏ธ โš™๏ธ ๐ŸŽ“ ๐Ÿ”—) ๐Ÿ‘ˆ ๐Ÿ‘† ๐Ÿ‘จโ€๐ŸŽจ ๐Ÿšซ โ„ข โŒ ๐Ÿ”ƒ ๐Ÿ‘ซ ๐Ÿ†Ž.

๐Ÿ‘ˆ ๐ŸŒŒ ๐Ÿ‘† ๐Ÿ’ช โš™๏ธ ๐Ÿ‘† ๐Ÿ˜ ๐Ÿ‘จโ€๐ŸŽจ & ๐Ÿ› ๏ธ ๐Ÿงฐ ๐Ÿต โœ”๏ธ ๐Ÿšฎ ๐Ÿ›ƒ ๐Ÿ“ณ ๐Ÿคทโ€โ™‚ ๐Ÿ“š โŒ.