Skip to content

๐Ÿ’ช - ๐Ÿ’— ๐Ÿ”ข

๐Ÿ”œ ๐Ÿ‘ˆ ๐Ÿ‘ฅ โœ”๏ธ ๐Ÿ‘€ โ” โš™๏ธ Path & Query, โžก๏ธ ๐Ÿ‘€ ๐ŸŒ… ๐Ÿง โš™๏ธ ๐Ÿ“จ ๐Ÿ’ช ๐Ÿ“„.

๐ŸŒ€ Path, Query & ๐Ÿ’ช ๐Ÿ”ข

๐Ÿฅ‡, โ†—๏ธ, ๐Ÿ‘† ๐Ÿ’ช ๐ŸŒ€ Path, Query & ๐Ÿ“จ ๐Ÿ’ช ๐Ÿ”ข ๐Ÿ“„ โžก & FastAPI ๐Ÿ”œ ๐Ÿ’ญ โšซ๏ธโ”.

& ๐Ÿ‘† ๐Ÿ’ช ๐Ÿ“ฃ ๐Ÿ’ช ๐Ÿ”ข ๐Ÿ“ฆ, โš’ ๐Ÿ”ข None:

from typing import Union

from fastapi import FastAPI, Path
from pydantic import BaseModel

app = FastAPI()


class Item(BaseModel):
    name: str
    description: Union[str, None] = None
    price: float
    tax: Union[float, None] = None


@app.put("/items/{item_id}")
async def update_item(
    *,
    item_id: int = Path(title="The ID of the item to get", ge=0, le=1000),
    q: Union[str, None] = None,
    item: Union[Item, None] = None,
):
    results = {"item_id": item_id}
    if q:
        results.update({"q": q})
    if item:
        results.update({"item": item})
    return results
from fastapi import FastAPI, Path
from pydantic import BaseModel

app = FastAPI()


class Item(BaseModel):
    name: str
    description: str | None = None
    price: float
    tax: float | None = None


@app.put("/items/{item_id}")
async def update_item(
    *,
    item_id: int = Path(title="The ID of the item to get", ge=0, le=1000),
    q: str | None = None,
    item: Item | None = None,
):
    results = {"item_id": item_id}
    if q:
        results.update({"q": q})
    if item:
        results.update({"item": item})
    return results

Note

๐Ÿ‘€ ๐Ÿ‘ˆ, ๐Ÿ‘‰ ๐Ÿ’ผ, item ๐Ÿ‘ˆ ๐Ÿ”œ โœŠ โšช๏ธโžก๏ธ ๐Ÿ’ช ๐Ÿ“ฆ. โšซ๏ธ โœ”๏ธ None ๐Ÿ”ข ๐Ÿ’ฒ.

๐Ÿ’— ๐Ÿ’ช ๐Ÿ”ข

โฎ๏ธ ๐Ÿ–ผ, โžก ๐Ÿ› ๏ธ ๐Ÿ”œ โŒ› ๐ŸŽป ๐Ÿ’ช โฎ๏ธ ๐Ÿ”ข Item, ๐Ÿ’–:

{
    "name": "Foo",
    "description": "The pretender",
    "price": 42.0,
    "tax": 3.2
}

โœ‹๏ธ ๐Ÿ‘† ๐Ÿ’ช ๐Ÿ“ฃ ๐Ÿ’— ๐Ÿ’ช ๐Ÿ”ข, โœ… item & user:

from typing import Union

from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()


class Item(BaseModel):
    name: str
    description: Union[str, None] = None
    price: float
    tax: Union[float, None] = None


class User(BaseModel):
    username: str
    full_name: Union[str, None] = None


@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item, user: User):
    results = {"item_id": item_id, "item": item, "user": user}
    return results
from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()


class Item(BaseModel):
    name: str
    description: str | None = None
    price: float
    tax: float | None = None


class User(BaseModel):
    username: str
    full_name: str | None = None


@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item, user: User):
    results = {"item_id": item_id, "item": item, "user": user}
    return results

๐Ÿ‘‰ ๐Ÿ’ผ, FastAPI ๐Ÿ”œ ๐Ÿ‘€ ๐Ÿ‘ˆ ๐Ÿ“ค ๐ŸŒ… ๐ŸŒ˜ 1๏ธโƒฃ ๐Ÿ’ช ๐Ÿ”ข ๐Ÿ”ข (2๏ธโƒฃ ๐Ÿ”ข ๐Ÿ‘ˆ Pydantic ๐Ÿท).

, โšซ๏ธ ๐Ÿ”œ โคด๏ธ โš™๏ธ ๐Ÿ”ข ๐Ÿ“› ๐Ÿ”‘ (๐Ÿ‘ ๐Ÿ“›) ๐Ÿ’ช, & โŒ› ๐Ÿ’ช ๐Ÿ’–:

{
    "item": {
        "name": "Foo",
        "description": "The pretender",
        "price": 42.0,
        "tax": 3.2
    },
    "user": {
        "username": "dave",
        "full_name": "Dave Grohl"
    }
}

Note

๐Ÿ‘€ ๐Ÿ‘ˆ โœ‹๏ธ item ๐Ÿ“ฃ ๐ŸŽ ๐ŸŒŒ โญ, โšซ๏ธ ๐Ÿ”œ โŒ› ๐Ÿ”˜ ๐Ÿ’ช โฎ๏ธ ๐Ÿ”‘ item.

FastAPI ๐Ÿ”œ ๐Ÿง ๐Ÿ› ๏ธ โšช๏ธโžก๏ธ ๐Ÿ“จ, ๐Ÿ‘ˆ ๐Ÿ”ข item ๐Ÿ“จ โšซ๏ธ ๐ŸŽฏ ๐ŸŽš & ๐ŸŽ user.

โšซ๏ธ ๐Ÿ”œ ๐ŸŽญ ๐Ÿ”ฌ โš— ๐Ÿ’ฝ, & ๐Ÿ”œ ๐Ÿ“„ โšซ๏ธ ๐Ÿ’– ๐Ÿ‘ˆ ๐Ÿ—„ ๐Ÿ”— & ๐Ÿง ๐Ÿฉบ.

โญ ๐Ÿ’ฒ ๐Ÿ’ช

๐ŸŽ ๐ŸŒŒ ๐Ÿ“ค Query & Path ๐Ÿ”ฌ โž• ๐Ÿ’ฝ ๐Ÿ”ข & โžก ๐Ÿ”ข, FastAPI ๐Ÿšš ๐ŸŒ“ Body.

๐Ÿ–ผ, โ†” โฎ๏ธ ๐Ÿท, ๐Ÿ‘† ๐Ÿ’ช ๐Ÿ’ญ ๐Ÿ‘ˆ ๐Ÿ‘† ๐Ÿ’š โœ”๏ธ โž•1๏ธโƒฃ ๐Ÿ”‘ importance ๐ŸŽ ๐Ÿ’ช, ๐Ÿฅˆ item & user.

๐Ÿšฅ ๐Ÿ‘† ๐Ÿ“ฃ โšซ๏ธ, โ†ฉ๏ธ โšซ๏ธ โญ ๐Ÿ’ฒ, FastAPI ๐Ÿ”œ ๐Ÿค” ๐Ÿ‘ˆ โšซ๏ธ ๐Ÿ”ข ๐Ÿ”ข.

โœ‹๏ธ ๐Ÿ‘† ๐Ÿ’ช ๐Ÿ’ก FastAPI ๐Ÿ˜ฅ โšซ๏ธ โž•1๏ธโƒฃ ๐Ÿ’ช ๐Ÿ”‘ โš™๏ธ Body:

from typing import Union

from fastapi import Body, FastAPI
from pydantic import BaseModel

app = FastAPI()


class Item(BaseModel):
    name: str
    description: Union[str, None] = None
    price: float
    tax: Union[float, None] = None


class User(BaseModel):
    username: str
    full_name: Union[str, None] = None


@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item, user: User, importance: int = Body()):
    results = {"item_id": item_id, "item": item, "user": user, "importance": importance}
    return results
from fastapi import Body, FastAPI
from pydantic import BaseModel

app = FastAPI()


class Item(BaseModel):
    name: str
    description: str | None = None
    price: float
    tax: float | None = None


class User(BaseModel):
    username: str
    full_name: str | None = None


@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item, user: User, importance: int = Body()):
    results = {"item_id": item_id, "item": item, "user": user, "importance": importance}
    return results

๐Ÿ‘‰ ๐Ÿ’ผ, FastAPI ๐Ÿ”œ โŒ› ๐Ÿ’ช ๐Ÿ’–:

{
    "item": {
        "name": "Foo",
        "description": "The pretender",
        "price": 42.0,
        "tax": 3.2
    },
    "user": {
        "username": "dave",
        "full_name": "Dave Grohl"
    },
    "importance": 5
}

๐Ÿ”„, โšซ๏ธ ๐Ÿ”œ ๐Ÿ—œ ๐Ÿ“Š ๐Ÿ†Ž, โœ”, ๐Ÿ“„, โ™’๏ธ.

๐Ÿ’— ๐Ÿ’ช = & ๐Ÿ”ข

โ†—๏ธ, ๐Ÿ‘† ๐Ÿ’ช ๐Ÿ“ฃ ๐ŸŒ– ๐Ÿ”ข ๐Ÿ”ข ๐Ÿ•โ” ๐Ÿ‘† ๐Ÿ’ช, ๐ŸŒ– ๐Ÿ™† ๐Ÿ’ช ๐Ÿ”ข.

, ๐Ÿ”ข, โญ ๐Ÿ’ฒ ๐Ÿ”ฌ ๐Ÿ”ข ๐Ÿ”ข, ๐Ÿ‘† ๐Ÿšซ โœ”๏ธ ๐ŸŽฏ ๐Ÿšฎ Query, ๐Ÿ‘† ๐Ÿ’ช:

q: Union[str, None] = None

โš–๏ธ ๐Ÿ 3๏ธโƒฃ.1๏ธโƒฃ0๏ธโƒฃ & ๐Ÿ”›:

q: str | None = None

๐Ÿ–ผ:

from typing import Union

from fastapi import Body, FastAPI
from pydantic import BaseModel

app = FastAPI()


class Item(BaseModel):
    name: str
    description: Union[str, None] = None
    price: float
    tax: Union[float, None] = None


class User(BaseModel):
    username: str
    full_name: Union[str, None] = None


@app.put("/items/{item_id}")
async def update_item(
    *,
    item_id: int,
    item: Item,
    user: User,
    importance: int = Body(gt=0),
    q: Union[str, None] = None,
):
    results = {"item_id": item_id, "item": item, "user": user, "importance": importance}
    if q:
        results.update({"q": q})
    return results
from fastapi import Body, FastAPI
from pydantic import BaseModel

app = FastAPI()


class Item(BaseModel):
    name: str
    description: str | None = None
    price: float
    tax: float | None = None


class User(BaseModel):
    username: str
    full_name: str | None = None


@app.put("/items/{item_id}")
async def update_item(
    *,
    item_id: int,
    item: Item,
    user: User,
    importance: int = Body(gt=0),
    q: str | None = None,
):
    results = {"item_id": item_id, "item": item, "user": user, "importance": importance}
    if q:
        results.update({"q": q})
    return results

Info

Body โœ”๏ธ ๐ŸŒ ๐ŸŽ โž• ๐Ÿ”ฌ & ๐Ÿ—ƒ ๐Ÿ”ข Query,Path & ๐ŸŽ ๐Ÿ‘† ๐Ÿ”œ ๐Ÿ‘€ โช.

โฏ ๐Ÿ‘ ๐Ÿ’ช ๐Ÿ”ข

โžก๏ธ ๐Ÿ’ฌ ๐Ÿ‘† ๐Ÿ•ด โœ”๏ธ ๐Ÿ‘ item ๐Ÿ’ช ๐Ÿ”ข โšช๏ธโžก๏ธ Pydantic ๐Ÿท Item.

๐Ÿ”ข, FastAPI ๐Ÿ”œ โคด๏ธ โŒ› ๐Ÿšฎ ๐Ÿ’ช ๐Ÿ”—.

โœ‹๏ธ ๐Ÿšฅ ๐Ÿ‘† ๐Ÿ’š โšซ๏ธ โŒ› ๐ŸŽป โฎ๏ธ ๐Ÿ”‘ item & ๐Ÿ”˜ โšซ๏ธ ๐Ÿท ๐ŸŽš, โšซ๏ธ ๐Ÿ”จ ๐Ÿ•โ” ๐Ÿ‘† ๐Ÿ“ฃ โž• ๐Ÿ’ช ๐Ÿ”ข, ๐Ÿ‘† ๐Ÿ’ช โš™๏ธ ๐ŸŽ Body ๐Ÿ”ข embed:

item: Item = Body(embed=True)

:

from typing import Union

from fastapi import Body, FastAPI
from pydantic import BaseModel

app = FastAPI()


class Item(BaseModel):
    name: str
    description: Union[str, None] = None
    price: float
    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

app = FastAPI()


class Item(BaseModel):
    name: str
    description: str | None = None
    price: float
    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

๐Ÿ‘‰ ๐Ÿ’ผ FastAPI ๐Ÿ”œ โŒ› ๐Ÿ’ช ๐Ÿ’–:

{
    "item": {
        "name": "Foo",
        "description": "The pretender",
        "price": 42.0,
        "tax": 3.2
    }
}

โ†ฉ๏ธ:

{
    "name": "Foo",
    "description": "The pretender",
    "price": 42.0,
    "tax": 3.2
}

๐ŸŒƒ

๐Ÿ‘† ๐Ÿ’ช ๐Ÿšฎ ๐Ÿ’— ๐Ÿ’ช ๐Ÿ”ข ๐Ÿ‘† โžก ๐Ÿ› ๏ธ ๐Ÿ”ข, โœ‹๏ธ ๐Ÿ“จ ๐Ÿ’ช ๐Ÿ•ด โœ”๏ธ ๐Ÿ‘ ๐Ÿ’ช.

โœ‹๏ธ FastAPI ๐Ÿ”œ ๐Ÿต โšซ๏ธ, ๐Ÿค ๐Ÿ‘† โ˜‘ ๐Ÿ“Š ๐Ÿ‘† ๐Ÿ”ข, & โœ” & ๐Ÿ“„ โ˜‘ ๐Ÿ”— โžก ๐Ÿ› ๏ธ.

๐Ÿ‘† ๐Ÿ’ช ๐Ÿ“ฃ โญ ๐Ÿ’ฒ ๐Ÿ“จ ๐Ÿ• ๐Ÿ’ช.

& ๐Ÿ‘† ๐Ÿ’ช ๐Ÿ’ก FastAPI โฏ ๐Ÿ’ช ๐Ÿ”‘ ๐Ÿ•โ” ๐Ÿ“ค ๐Ÿ•ด ๐Ÿ‘ ๐Ÿ”ข ๐Ÿ“ฃ.