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Ⓜ 🎏 🌌 ⏮️ ➡ 🔢.