Skip to content

๐Ÿ”ข ๐Ÿ”ข

๐Ÿ•โ” ๐Ÿ‘† ๐Ÿ“ฃ ๐ŸŽ ๐Ÿ”ข ๐Ÿ”ข ๐Ÿ‘ˆ ๐Ÿšซ ๐Ÿ• โžก ๐Ÿ”ข, ๐Ÿ‘ซ ๐Ÿ” ๐Ÿ”ฌ "๐Ÿ”ข" ๐Ÿ”ข.

from fastapi import FastAPI

app = FastAPI()

fake_items_db = [{"item_name": "Foo"}, {"item_name": "Bar"}, {"item_name": "Baz"}]


@app.get("/items/")
async def read_item(skip: int = 0, limit: int = 10):
    return fake_items_db[skip : skip + limit]

๐Ÿ”ข โš’ ๐Ÿ”‘-๐Ÿ’ฒ ๐Ÿ‘ซ ๐Ÿ‘ˆ ๐Ÿšถ โฎ๏ธ ? ๐Ÿ“›, ๐ŸŽ & ๐Ÿฆน.

๐Ÿ–ผ, ๐Ÿ“›:

http://127.0.0.1:8000/items/?skip=0&limit=10

...๐Ÿ”ข ๐Ÿ”ข:

  • skip: โฎ๏ธ ๐Ÿ’ฒ 0
  • limit: โฎ๏ธ ๐Ÿ’ฒ 10

๐Ÿ‘ซ ๐Ÿ• ๐Ÿ“›, ๐Ÿ‘ซ "๐Ÿ›Ž" ๐ŸŽป.

โœ‹๏ธ ๐Ÿ•โ” ๐Ÿ‘† ๐Ÿ“ฃ ๐Ÿ‘ซ โฎ๏ธ ๐Ÿ ๐Ÿ†Ž (๐Ÿ–ผ ๐Ÿ”›, int), ๐Ÿ‘ซ ๐Ÿ—œ ๐Ÿ‘ˆ ๐Ÿ†Ž & โœ” ๐Ÿ›ก โšซ๏ธ.

๐ŸŒ ๐ŸŽ ๐Ÿ› ๏ธ ๐Ÿ‘ˆ โš– โžก ๐Ÿ”ข โœ” ๐Ÿ”ข ๐Ÿ”ข:

  • ๐Ÿ‘จโ€๐ŸŽจ ๐Ÿ•โ€๐Ÿฆบ (๐ŸŽฒ)
  • ๐Ÿ’ฝ "โœ"
  • ๐Ÿ’ฝ ๐Ÿ”ฌ
  • ๐Ÿง ๐Ÿงพ

๐Ÿ”ข

๐Ÿ”ข ๐Ÿ”ข ๐Ÿšซ ๐Ÿ”ง ๐Ÿ• โžก, ๐Ÿ‘ซ ๐Ÿ’ช ๐Ÿ“ฆ & ๐Ÿ’ช โœ”๏ธ ๐Ÿ”ข ๐Ÿ’ฒ.

๐Ÿ–ผ ๐Ÿ”› ๐Ÿ‘ซ โœ”๏ธ ๐Ÿ”ข ๐Ÿ’ฒ skip=0 & limit=10.

, ๐Ÿ”œ ๐Ÿ“›:

http://127.0.0.1:8000/items/

๐Ÿ”œ ๐ŸŽ ๐Ÿ”œ:

http://127.0.0.1:8000/items/?skip=0&limit=10

โœ‹๏ธ ๐Ÿšฅ ๐Ÿ‘† ๐Ÿšถ, ๐Ÿ–ผ:

http://127.0.0.1:8000/items/?skip=20

๐Ÿ”ข ๐Ÿ’ฒ ๐Ÿ‘† ๐Ÿ”ข ๐Ÿ”œ:

  • skip=20: โ†ฉ๏ธ ๐Ÿ‘† โš’ โšซ๏ธ ๐Ÿ“›
  • limit=10: โ†ฉ๏ธ ๐Ÿ‘ˆ ๐Ÿ”ข ๐Ÿ’ฒ

๐Ÿ“ฆ ๐Ÿ”ข

๐ŸŽ ๐ŸŒŒ, ๐Ÿ‘† ๐Ÿ’ช ๐Ÿ“ฃ ๐Ÿ“ฆ ๐Ÿ”ข ๐Ÿ”ข, โš’ ๐Ÿ‘ซ ๐Ÿ”ข None:

from typing import Union

from fastapi import FastAPI

app = FastAPI()


@app.get("/items/{item_id}")
async def read_item(item_id: str, q: Union[str, None] = None):
    if q:
        return {"item_id": item_id, "q": q}
    return {"item_id": item_id}
from fastapi import FastAPI

app = FastAPI()


@app.get("/items/{item_id}")
async def read_item(item_id: str, q: str | None = None):
    if q:
        return {"item_id": item_id, "q": q}
    return {"item_id": item_id}

๐Ÿ‘‰ ๐Ÿ’ผ, ๐Ÿ”ข ๐Ÿ”ข q ๐Ÿ”œ ๐Ÿ“ฆ, & ๐Ÿ”œ None ๐Ÿ”ข.

Check

๐Ÿ‘€ ๐Ÿ‘ˆ FastAPI ๐Ÿ™ƒ ๐Ÿฅƒ ๐Ÿ‘€ ๐Ÿ‘ˆ โžก ๐Ÿ”ข item_id โžก ๐Ÿ”ข & q ๐Ÿšซ,, โšซ๏ธ ๐Ÿ”ข ๐Ÿ”ข.

๐Ÿ”ข ๐Ÿ”ข ๐Ÿ†Ž ๐Ÿ› ๏ธ

๐Ÿ‘† ๐Ÿ’ช ๐Ÿ“ฃ bool ๐Ÿ†Ž, & ๐Ÿ‘ซ ๐Ÿ”œ ๐Ÿ—œ:

from typing import Union

from fastapi import FastAPI

app = FastAPI()


@app.get("/items/{item_id}")
async def read_item(item_id: str, q: Union[str, None] = None, short: bool = False):
    item = {"item_id": item_id}
    if q:
        item.update({"q": q})
    if not short:
        item.update(
            {"description": "This is an amazing item that has a long description"}
        )
    return item
from fastapi import FastAPI

app = FastAPI()


@app.get("/items/{item_id}")
async def read_item(item_id: str, q: str | None = None, short: bool = False):
    item = {"item_id": item_id}
    if q:
        item.update({"q": q})
    if not short:
        item.update(
            {"description": "This is an amazing item that has a long description"}
        )
    return item

๐Ÿ‘‰ ๐Ÿ’ผ, ๐Ÿšฅ ๐Ÿ‘† ๐Ÿšถ:

http://127.0.0.1:8000/items/foo?short=1

โš–๏ธ

http://127.0.0.1:8000/items/foo?short=True

โš–๏ธ

http://127.0.0.1:8000/items/foo?short=true

โš–๏ธ

http://127.0.0.1:8000/items/foo?short=on

โš–๏ธ

http://127.0.0.1:8000/items/foo?short=yes

โš–๏ธ ๐Ÿ™† ๐ŸŽ ๐Ÿ’ผ ๐Ÿ“ˆ (๐Ÿ” , ๐Ÿฅ‡ ๐Ÿ”ค ๐Ÿ” , โ™’๏ธ), ๐Ÿ‘† ๐Ÿ”ข ๐Ÿ”œ ๐Ÿ‘€ ๐Ÿ”ข short โฎ๏ธ bool ๐Ÿ’ฒ True. โช False.

๐Ÿ’— โžก & ๐Ÿ”ข ๐Ÿ”ข

๐Ÿ‘† ๐Ÿ’ช ๐Ÿ“ฃ ๐Ÿ’— โžก ๐Ÿ”ข & ๐Ÿ”ข ๐Ÿ”ข ๐ŸŽ ๐Ÿ•ฐ, FastAPI ๐Ÿ’ญ โ” โ”.

& ๐Ÿ‘† ๐Ÿšซ โœ”๏ธ ๐Ÿ“ฃ ๐Ÿ‘ซ ๐Ÿ™† ๐ŸŽฏ โœ”.

๐Ÿ‘ซ ๐Ÿ”œ ๐Ÿ”ฌ ๐Ÿ“›:

from typing import Union

from fastapi import FastAPI

app = FastAPI()


@app.get("/users/{user_id}/items/{item_id}")
async def read_user_item(
    user_id: int, item_id: str, q: Union[str, None] = None, short: bool = False
):
    item = {"item_id": item_id, "owner_id": user_id}
    if q:
        item.update({"q": q})
    if not short:
        item.update(
            {"description": "This is an amazing item that has a long description"}
        )
    return item
from fastapi import FastAPI

app = FastAPI()


@app.get("/users/{user_id}/items/{item_id}")
async def read_user_item(
    user_id: int, item_id: str, q: str | None = None, short: bool = False
):
    item = {"item_id": item_id, "owner_id": user_id}
    if q:
        item.update({"q": q})
    if not short:
        item.update(
            {"description": "This is an amazing item that has a long description"}
        )
    return item

โœ” ๐Ÿ”ข ๐Ÿ”ข

๐Ÿ•โ” ๐Ÿ‘† ๐Ÿ“ฃ ๐Ÿ”ข ๐Ÿ’ฒ ๐Ÿšซ-โžก ๐Ÿ”ข (๐Ÿ”œ, ๐Ÿ‘ฅ โœ”๏ธ ๐Ÿ•ด ๐Ÿ‘€ ๐Ÿ”ข ๐Ÿ”ข), โคด๏ธ โšซ๏ธ ๐Ÿšซ โœ”.

๐Ÿšฅ ๐Ÿ‘† ๐Ÿšซ ๐Ÿ’š ๐Ÿšฎ ๐ŸŽฏ ๐Ÿ’ฒ โœ‹๏ธ โš’ โšซ๏ธ ๐Ÿ“ฆ, โš’ ๐Ÿ”ข None.

โœ‹๏ธ ๐Ÿ•โ” ๐Ÿ‘† ๐Ÿ’š โš’ ๐Ÿ”ข ๐Ÿ”ข โœ”, ๐Ÿ‘† ๐Ÿ’ช ๐Ÿšซ ๐Ÿ“ฃ ๐Ÿ™† ๐Ÿ”ข ๐Ÿ’ฒ:

from fastapi import FastAPI

app = FastAPI()


@app.get("/items/{item_id}")
async def read_user_item(item_id: str, needy: str):
    item = {"item_id": item_id, "needy": needy}
    return item

๐Ÿ“ฅ ๐Ÿ”ข ๐Ÿ”ข needy โœ” ๐Ÿ”ข ๐Ÿ”ข ๐Ÿ†Ž str.

๐Ÿšฅ ๐Ÿ‘† ๐Ÿ“‚ ๐Ÿ‘† ๐Ÿ–ฅ ๐Ÿ“› ๐Ÿ’–:

http://127.0.0.1:8000/items/foo-item

...๐Ÿต โŽ โœ” ๐Ÿ”ข needy, ๐Ÿ‘† ๐Ÿ”œ ๐Ÿ‘€ โŒ ๐Ÿ’–:

{
    "detail": [
        {
            "loc": [
                "query",
                "needy"
            ],
            "msg": "field required",
            "type": "value_error.missing"
        }
    ]
}

needy ๐Ÿšš ๐Ÿ”ข, ๐Ÿ‘† ๐Ÿ”œ ๐Ÿ’ช โš’ โšซ๏ธ ๐Ÿ“›:

http://127.0.0.1:8000/items/foo-item?needy=sooooneedy

...๐Ÿ‘‰ ๐Ÿ”œ ๐Ÿ‘ท:

{
    "item_id": "foo-item",
    "needy": "sooooneedy"
}

& โ†—๏ธ, ๐Ÿ‘† ๐Ÿ’ช ๐Ÿ”ฌ ๐Ÿ”ข โœ”, โœ”๏ธ ๐Ÿ”ข ๐Ÿ’ฒ, & ๐Ÿ• ๐Ÿ“ฆ:

from typing import Union

from fastapi import FastAPI

app = FastAPI()


@app.get("/items/{item_id}")
async def read_user_item(
    item_id: str, needy: str, skip: int = 0, limit: Union[int, None] = None
):
    item = {"item_id": item_id, "needy": needy, "skip": skip, "limit": limit}
    return item
from fastapi import FastAPI

app = FastAPI()


@app.get("/items/{item_id}")
async def read_user_item(
    item_id: str, needy: str, skip: int = 0, limit: int | None = None
):
    item = {"item_id": item_id, "needy": needy, "skip": skip, "limit": limit}
    return item

๐Ÿ‘‰ ๐Ÿ’ผ, ๐Ÿ“ค 3๏ธโƒฃ ๐Ÿ”ข ๐Ÿ”ข:

  • needy, โœ” str.
  • skip, int โฎ๏ธ ๐Ÿ”ข ๐Ÿ’ฒ 0.
  • limit, ๐Ÿ“ฆ int.

Tip

๐Ÿ‘† ๐Ÿ’ช โš™๏ธ Enumโ“‚ ๐ŸŽ ๐ŸŒŒ โฎ๏ธ โžก ๐Ÿ”ข.