Skip to content

๐Ÿ“จ ๐Ÿท - ๐Ÿ“จ ๐Ÿ†Ž

๐Ÿ‘† ๐Ÿ’ช ๐Ÿ“ฃ ๐Ÿ†Ž โš™๏ธ ๐Ÿ“จ โœ โžก ๐Ÿ› ๏ธ ๐Ÿ”ข ๐Ÿ“จ ๐Ÿ†Ž.

๐Ÿ‘† ๐Ÿ’ช โš™๏ธ ๐Ÿ†Ž โœ ๐ŸŽ ๐ŸŒŒ ๐Ÿ‘† ๐Ÿ”œ ๐Ÿ”ข ๐Ÿ’ฝ ๐Ÿ”ข ๐Ÿ”ข, ๐Ÿ‘† ๐Ÿ’ช โš™๏ธ Pydantic ๐Ÿท, ๐Ÿ“‡, ๐Ÿ“–, ๐Ÿ“Š ๐Ÿ’ฒ ๐Ÿ’– ๐Ÿ”ข, ๐ŸŽป, โ™’๏ธ.

from typing import List, 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
    tags: List[str] = []


@app.post("/items/")
async def create_item(item: Item) -> Item:
    return item


@app.get("/items/")
async def read_items() -> List[Item]:
    return [
        Item(name="Portal Gun", price=42.0),
        Item(name="Plumbus", price=32.0),
    ]
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
    tags: list[str] = []


@app.post("/items/")
async def create_item(item: Item) -> Item:
    return item


@app.get("/items/")
async def read_items() -> list[Item]:
    return [
        Item(name="Portal Gun", price=42.0),
        Item(name="Plumbus", price=32.0),
    ]
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
    tags: list[str] = []


@app.post("/items/")
async def create_item(item: Item) -> Item:
    return item


@app.get("/items/")
async def read_items() -> list[Item]:
    return [
        Item(name="Portal Gun", price=42.0),
        Item(name="Plumbus", price=32.0),
    ]

FastAPI ๐Ÿ”œ โš™๏ธ ๐Ÿ‘‰ ๐Ÿ“จ ๐Ÿ†Ž:

  • โœ” ๐Ÿ“จ ๐Ÿ’ฝ.
    • ๐Ÿšฅ ๐Ÿ’ฝ โŒ (โœ… ๐Ÿ‘† โŒ ๐Ÿ‘), โšซ๏ธ โ›“ ๐Ÿ‘ˆ ๐Ÿ‘† ๐Ÿ“ฑ ๐Ÿ“Ÿ ๐Ÿ’”, ๐Ÿšซ ๐Ÿ›ฌ โšซ๏ธโ” โšซ๏ธ ๐Ÿ”œ, & โšซ๏ธ ๐Ÿ”œ ๐Ÿ“จ ๐Ÿ’ฝ โŒ โ†ฉ๏ธ ๐Ÿ›ฌ โŒ ๐Ÿ’ฝ. ๐Ÿ‘‰ ๐ŸŒŒ ๐Ÿ‘† & ๐Ÿ‘† ๐Ÿ‘ฉโ€๐Ÿ’ป ๐Ÿ’ช ๐ŸŽฏ ๐Ÿ‘ˆ ๐Ÿ‘ซ ๐Ÿ”œ ๐Ÿ“จ ๐Ÿ’ฝ & ๐Ÿ’ฝ ๐Ÿ’  ๐Ÿ“ˆ.
  • ๐Ÿšฎ ๐ŸŽป ๐Ÿ”— ๐Ÿ“จ, ๐Ÿ—„ โžก ๐Ÿ› ๏ธ.
    • ๐Ÿ‘‰ ๐Ÿ”œ โš™๏ธ ๐Ÿง ๐Ÿฉบ.
    • โšซ๏ธ ๐Ÿ”œ โš™๏ธ ๐Ÿง ๐Ÿ‘ฉโ€๐Ÿ’ป ๐Ÿ“Ÿ โšก ๐Ÿงฐ.

โœ‹๏ธ ๐Ÿ† ๐Ÿฅˆ:

  • โšซ๏ธ ๐Ÿ”œ ๐Ÿ“‰ & โ›ฝ ๐Ÿ”ข ๐Ÿ“Š โšซ๏ธโ” ๐Ÿ”ฌ ๐Ÿ“จ ๐Ÿ†Ž.
    • ๐Ÿ‘‰ โœด๏ธ โš  ๐Ÿ’‚โ€โ™‚, ๐Ÿ‘ฅ ๐Ÿ”œ ๐Ÿ‘€ ๐ŸŒ… ๐Ÿ‘ˆ ๐Ÿ”›.

response_model ๐Ÿ”ข

๐Ÿ“ค ๐Ÿ’ผ ๐ŸŒโ” ๐Ÿ‘† ๐Ÿ’ช โš–๏ธ ๐Ÿ’š ๐Ÿ“จ ๐Ÿ’ฝ ๐Ÿ‘ˆ ๐Ÿšซ โšซ๏ธโ” โšซ๏ธโ” ๐Ÿ†Ž ๐Ÿ“ฃ.

๐Ÿ–ผ, ๐Ÿ‘† ๐Ÿ’ช ๐Ÿ’š ๐Ÿ“จ ๐Ÿ“– โš–๏ธ ๐Ÿ’ฝ ๐ŸŽš, โœ‹๏ธ ๐Ÿ“ฃ โšซ๏ธ Pydantic ๐Ÿท. ๐Ÿ‘‰ ๐ŸŒŒ Pydantic ๐Ÿท ๐Ÿ”œ ๐ŸŒ ๐Ÿ’ฝ ๐Ÿงพ, ๐Ÿ”ฌ, โ™’๏ธ. ๐ŸŽš ๐Ÿ‘ˆ ๐Ÿ‘† ๐Ÿ“จ (โœ… ๐Ÿ“– โš–๏ธ ๐Ÿ’ฝ ๐ŸŽš).

๐Ÿšฅ ๐Ÿ‘† ๐Ÿšฎ ๐Ÿ“จ ๐Ÿ†Ž โœ, ๐Ÿงฐ & ๐Ÿ‘จโ€๐ŸŽจ ๐Ÿ”œ ๐Ÿ˜ญ โฎ๏ธ (โ˜‘) โŒ ๐Ÿ’ฌ ๐Ÿ‘† ๐Ÿ‘ˆ ๐Ÿ‘† ๐Ÿ”ข ๐Ÿ›ฌ ๐Ÿ†Ž (โœ…#๏ธโƒฃ) ๐Ÿ‘ˆ ๐ŸŽ โšช๏ธโžก๏ธ โšซ๏ธโ” ๐Ÿ‘† ๐Ÿ“ฃ (โœ… Pydantic ๐Ÿท).

๐Ÿ“š ๐Ÿ’ผ, ๐Ÿ‘† ๐Ÿ’ช โš™๏ธ โžก ๐Ÿ› ๏ธ ๐Ÿ‘จโ€๐ŸŽจ ๐Ÿ”ข response_model โ†ฉ๏ธ ๐Ÿ“จ ๐Ÿ†Ž.

๐Ÿ‘† ๐Ÿ’ช โš™๏ธ response_model ๐Ÿ”ข ๐Ÿ™† โžก ๐Ÿ› ๏ธ:

  • @app.get()
  • @app.post()
  • @app.put()
  • @app.delete()
  • โ™’๏ธ.
from typing import Any, List, 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
    tags: List[str] = []


@app.post("/items/", response_model=Item)
async def create_item(item: Item) -> Any:
    return item


@app.get("/items/", response_model=List[Item])
async def read_items() -> Any:
    return [
        {"name": "Portal Gun", "price": 42.0},
        {"name": "Plumbus", "price": 32.0},
    ]
from typing import Any, 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
    tags: list[str] = []


@app.post("/items/", response_model=Item)
async def create_item(item: Item) -> Any:
    return item


@app.get("/items/", response_model=list[Item])
async def read_items() -> Any:
    return [
        {"name": "Portal Gun", "price": 42.0},
        {"name": "Plumbus", "price": 32.0},
    ]
from typing import Any

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
    tags: list[str] = []


@app.post("/items/", response_model=Item)
async def create_item(item: Item) -> Any:
    return item


@app.get("/items/", response_model=list[Item])
async def read_items() -> Any:
    return [
        {"name": "Portal Gun", "price": 42.0},
        {"name": "Plumbus", "price": 32.0},
    ]

Note

๐Ÿ‘€ ๐Ÿ‘ˆ response_model ๐Ÿ”ข "๐Ÿ‘จโ€๐ŸŽจ" ๐Ÿ‘ฉโ€๐Ÿ”ฌ (get, post, โ™’๏ธ). ๐Ÿšซ ๐Ÿ‘† โžก ๐Ÿ› ๏ธ ๐Ÿ”ข, ๐Ÿ’– ๐ŸŒ ๐Ÿ”ข & ๐Ÿ’ช.

response_model ๐Ÿ“จ ๐ŸŽ ๐Ÿ†Ž ๐Ÿ‘† ๐Ÿ”œ ๐Ÿ“ฃ Pydantic ๐Ÿท ๐Ÿ‘,, โšซ๏ธ ๐Ÿ’ช Pydantic ๐Ÿท, โœ‹๏ธ โšซ๏ธ ๐Ÿ’ช, โœ… list Pydantic ๐Ÿท, ๐Ÿ’– List[Item].

FastAPI ๐Ÿ”œ โš™๏ธ ๐Ÿ‘‰ response_model ๐ŸŒ ๐Ÿ’ฝ ๐Ÿงพ, ๐Ÿ”ฌ, โ™’๏ธ. & ๐Ÿ—œ & โ›ฝ ๐Ÿ”ข ๐Ÿ“Š ๐Ÿšฎ ๐Ÿ†Ž ๐Ÿ“„.

Tip

๐Ÿšฅ ๐Ÿ‘† โœ”๏ธ โš  ๐Ÿ†Ž โœ… ๐Ÿ‘† ๐Ÿ‘จโ€๐ŸŽจ, โœ, โ™’๏ธ, ๐Ÿ‘† ๐Ÿ’ช ๐Ÿ“ฃ ๐Ÿ”ข ๐Ÿ“จ ๐Ÿ†Ž Any.

๐Ÿ‘ˆ ๐ŸŒŒ ๐Ÿ‘† ๐Ÿ’ฌ ๐Ÿ‘จโ€๐ŸŽจ ๐Ÿ‘ˆ ๐Ÿ‘† ๐Ÿ˜ซ ๐Ÿ›ฌ ๐Ÿ•ณ. โœ‹๏ธ FastAPI ๐Ÿ”œ ๐Ÿ’ฝ ๐Ÿงพ, ๐Ÿ”ฌ, ๐Ÿ–ฅ, โ™’๏ธ. โฎ๏ธ response_model.

response_model ๐Ÿ“ซ

๐Ÿšฅ ๐Ÿ‘† ๐Ÿ“ฃ ๐Ÿ‘ฏโ€โ™‚๏ธ ๐Ÿ“จ ๐Ÿ†Ž & response_model, response_model ๐Ÿ”œ โœŠ ๐Ÿ“ซ & โš™๏ธ FastAPI.

๐Ÿ‘‰ ๐ŸŒŒ ๐Ÿ‘† ๐Ÿ’ช ๐Ÿšฎ โ˜‘ ๐Ÿ†Ž โœ ๐Ÿ‘† ๐Ÿ”ข ๐Ÿ•โ” ๐Ÿ‘† ๐Ÿ›ฌ ๐Ÿ†Ž ๐ŸŽ ๐ŸŒ˜ ๐Ÿ“จ ๐Ÿท, โš™๏ธ ๐Ÿ‘จโ€๐ŸŽจ & ๐Ÿงฐ ๐Ÿ’– โœ. & ๐Ÿ‘† ๐Ÿ’ช โœ”๏ธ FastAPI ๐Ÿ’ฝ ๐Ÿ”ฌ, ๐Ÿงพ, โ™’๏ธ. โš™๏ธ response_model.

๐Ÿ‘† ๐Ÿ’ช โš™๏ธ response_model=None โŽ ๐Ÿ— ๐Ÿ“จ ๐Ÿท ๐Ÿ‘ˆ โžก ๐Ÿ› ๏ธ, ๐Ÿ‘† 5๏ธโƒฃ๐Ÿ“† ๐Ÿ’ช โšซ๏ธ ๐Ÿšฅ ๐Ÿ‘† โŽ ๐Ÿ†Ž โœ ๐Ÿ‘œ ๐Ÿ‘ˆ ๐Ÿšซ โ˜‘ Pydantic ๐Ÿ‘, ๐Ÿ‘† ๐Ÿ”œ ๐Ÿ‘€ ๐Ÿ–ผ ๐Ÿ‘ˆ 1๏ธโƒฃ ๐Ÿ“„ ๐Ÿ”›.

๐Ÿ“จ ๐ŸŽ ๐Ÿ”ข ๐Ÿ’ฝ

๐Ÿ“ฅ ๐Ÿ‘ฅ ๐Ÿ“ฃ UserIn ๐Ÿท, โšซ๏ธ ๐Ÿ”œ ๐Ÿ”Œ ๐Ÿ”ข ๐Ÿ”:

from typing import Union

from fastapi import FastAPI
from pydantic import BaseModel, EmailStr

app = FastAPI()


class UserIn(BaseModel):
    username: str
    password: str
    email: EmailStr
    full_name: Union[str, None] = None


# Don't do this in production!
@app.post("/user/")
async def create_user(user: UserIn) -> UserIn:
    return user
from fastapi import FastAPI
from pydantic import BaseModel, EmailStr

app = FastAPI()


class UserIn(BaseModel):
    username: str
    password: str
    email: EmailStr
    full_name: str | None = None


# Don't do this in production!
@app.post("/user/")
async def create_user(user: UserIn) -> UserIn:
    return user

Info

โš™๏ธ EmailStr, ๐Ÿฅ‡ โŽ email_validator.

๐Ÿคถ โ“‚. pip install email-validator โš–๏ธ pip install pydantic[email].

& ๐Ÿ‘ฅ โš™๏ธ ๐Ÿ‘‰ ๐Ÿท ๐Ÿ“ฃ ๐Ÿ‘† ๐Ÿ”ข & ๐ŸŽ ๐Ÿท ๐Ÿ“ฃ ๐Ÿ‘† ๐Ÿ”ข:

from typing import Union

from fastapi import FastAPI
from pydantic import BaseModel, EmailStr

app = FastAPI()


class UserIn(BaseModel):
    username: str
    password: str
    email: EmailStr
    full_name: Union[str, None] = None


# Don't do this in production!
@app.post("/user/")
async def create_user(user: UserIn) -> UserIn:
    return user
from fastapi import FastAPI
from pydantic import BaseModel, EmailStr

app = FastAPI()


class UserIn(BaseModel):
    username: str
    password: str
    email: EmailStr
    full_name: str | None = None


# Don't do this in production!
@app.post("/user/")
async def create_user(user: UserIn) -> UserIn:
    return user

๐Ÿ”œ, ๐Ÿ•โ” ๐Ÿ–ฅ ๐Ÿ— ๐Ÿ‘ฉโ€๐Ÿ’ป โฎ๏ธ ๐Ÿ”, ๐Ÿ› ๏ธ ๐Ÿ”œ ๐Ÿ“จ ๐ŸŽ ๐Ÿ” ๐Ÿ“จ.

๐Ÿ‘‰ ๐Ÿ’ผ, โšซ๏ธ ๐Ÿ’ช ๐Ÿšซ โš , โ†ฉ๏ธ โšซ๏ธ ๐ŸŽ ๐Ÿ‘ฉโ€๐Ÿ’ป ๐Ÿ“จ ๐Ÿ”.

โœ‹๏ธ ๐Ÿšฅ ๐Ÿ‘ฅ โš™๏ธ ๐ŸŽ ๐Ÿท โž•1๏ธโƒฃ โžก ๐Ÿ› ๏ธ, ๐Ÿ‘ฅ ๐Ÿ’ช ๐Ÿ“จ ๐Ÿ‘† ๐Ÿ‘ฉโ€๐Ÿ’ป ๐Ÿ” ๐Ÿ”  ๐Ÿ‘ฉโ€๐Ÿ’ป.

Danger

๐Ÿ™… ๐Ÿช โœ… ๐Ÿ” ๐Ÿ‘ฉโ€๐Ÿ’ป โš–๏ธ ๐Ÿ“จ โšซ๏ธ ๐Ÿ“จ ๐Ÿ’– ๐Ÿ‘‰, ๐Ÿšฅ ๐Ÿ‘† ๐Ÿ’ญ ๐ŸŒ โš  & ๐Ÿ‘† ๐Ÿ’ญ โšซ๏ธโ” ๐Ÿ‘† ๐Ÿ”จ.

๐Ÿšฎ ๐Ÿ”ข ๐Ÿท

๐Ÿ‘ฅ ๐Ÿ’ช โ†ฉ๏ธ โœ ๐Ÿ”ข ๐Ÿท โฎ๏ธ ๐Ÿ”ข ๐Ÿ” & ๐Ÿ”ข ๐Ÿท ๐Ÿต โšซ๏ธ:

from typing import Any, Union

from fastapi import FastAPI
from pydantic import BaseModel, EmailStr

app = FastAPI()


class UserIn(BaseModel):
    username: str
    password: str
    email: EmailStr
    full_name: Union[str, None] = None


class UserOut(BaseModel):
    username: str
    email: EmailStr
    full_name: Union[str, None] = None


@app.post("/user/", response_model=UserOut)
async def create_user(user: UserIn) -> Any:
    return user
from typing import Any

from fastapi import FastAPI
from pydantic import BaseModel, EmailStr

app = FastAPI()


class UserIn(BaseModel):
    username: str
    password: str
    email: EmailStr
    full_name: str | None = None


class UserOut(BaseModel):
    username: str
    email: EmailStr
    full_name: str | None = None


@app.post("/user/", response_model=UserOut)
async def create_user(user: UserIn) -> Any:
    return user

๐Ÿ“ฅ, โœ‹๏ธ ๐Ÿ‘† โžก ๐Ÿ› ๏ธ ๐Ÿ”ข ๐Ÿ›ฌ ๐ŸŽ ๐Ÿ”ข ๐Ÿ‘ฉโ€๐Ÿ’ป ๐Ÿ‘ˆ ๐Ÿ”Œ ๐Ÿ”:

from typing import Any, Union

from fastapi import FastAPI
from pydantic import BaseModel, EmailStr

app = FastAPI()


class UserIn(BaseModel):
    username: str
    password: str
    email: EmailStr
    full_name: Union[str, None] = None


class UserOut(BaseModel):
    username: str
    email: EmailStr
    full_name: Union[str, None] = None


@app.post("/user/", response_model=UserOut)
async def create_user(user: UserIn) -> Any:
    return user
from typing import Any

from fastapi import FastAPI
from pydantic import BaseModel, EmailStr

app = FastAPI()


class UserIn(BaseModel):
    username: str
    password: str
    email: EmailStr
    full_name: str | None = None


class UserOut(BaseModel):
    username: str
    email: EmailStr
    full_name: str | None = None


@app.post("/user/", response_model=UserOut)
async def create_user(user: UserIn) -> Any:
    return user

...๐Ÿ‘ฅ ๐Ÿ“ฃ response_model ๐Ÿ‘† ๐Ÿท UserOut, ๐Ÿ‘ˆ ๐Ÿšซ ๐Ÿ”Œ ๐Ÿ”:

from typing import Any, Union

from fastapi import FastAPI
from pydantic import BaseModel, EmailStr

app = FastAPI()


class UserIn(BaseModel):
    username: str
    password: str
    email: EmailStr
    full_name: Union[str, None] = None


class UserOut(BaseModel):
    username: str
    email: EmailStr
    full_name: Union[str, None] = None


@app.post("/user/", response_model=UserOut)
async def create_user(user: UserIn) -> Any:
    return user
from typing import Any

from fastapi import FastAPI
from pydantic import BaseModel, EmailStr

app = FastAPI()


class UserIn(BaseModel):
    username: str
    password: str
    email: EmailStr
    full_name: str | None = None


class UserOut(BaseModel):
    username: str
    email: EmailStr
    full_name: str | None = None


@app.post("/user/", response_model=UserOut)
async def create_user(user: UserIn) -> Any:
    return user

, FastAPI ๐Ÿ”œ โœŠ ๐Ÿ’… ๐Ÿ–ฅ ๐Ÿ‘… ๐ŸŒ ๐Ÿ’ฝ ๐Ÿ‘ˆ ๐Ÿšซ ๐Ÿ“ฃ ๐Ÿ”ข ๐Ÿท (โš™๏ธ Pydantic).

response_model โš–๏ธ ๐Ÿ“จ ๐Ÿ†Ž

๐Ÿ‘‰ ๐Ÿ’ผ, โ†ฉ๏ธ 2๏ธโƒฃ ๐Ÿท ๐ŸŽ, ๐Ÿšฅ ๐Ÿ‘ฅ โœ ๐Ÿ”ข ๐Ÿ“จ ๐Ÿ†Ž UserOut, ๐Ÿ‘จโ€๐ŸŽจ & ๐Ÿงฐ ๐Ÿ”œ ๐Ÿ˜ญ ๐Ÿ‘ˆ ๐Ÿ‘ฅ ๐Ÿ›ฌ โŒ ๐Ÿ†Ž, ๐Ÿ“š ๐ŸŽ ๐ŸŽ“.

๐Ÿ‘ˆ โšซ๏ธโ” ๐Ÿ‘‰ ๐Ÿ–ผ ๐Ÿ‘ฅ โœ”๏ธ ๐Ÿ“ฃ โšซ๏ธ response_model ๐Ÿ”ข.

...โœ‹๏ธ ๐Ÿ˜ฃ ๐Ÿ‘‚ ๐Ÿ”› ๐Ÿ‘€ โ” โŽ ๐Ÿ‘ˆ.

๐Ÿ“จ ๐Ÿ†Ž & ๐Ÿ’ฝ ๐Ÿ–ฅ

โžก๏ธ ๐Ÿ˜ฃ โšช๏ธโžก๏ธ โฎ๏ธ ๐Ÿ–ผ. ๐Ÿ‘ฅ ๐Ÿ’š โœ ๐Ÿ”ข โฎ๏ธ 1๏ธโƒฃ ๐Ÿ†Ž โœ‹๏ธ ๐Ÿ“จ ๐Ÿ•ณ ๐Ÿ‘ˆ ๐Ÿ”Œ ๐ŸŒ… ๐Ÿ’ฝ.

๐Ÿ‘ฅ ๐Ÿ’š FastAPI ๐Ÿšง ๐Ÿ–ฅ ๐Ÿ“Š โš™๏ธ ๐Ÿ“จ ๐Ÿท.

โฎ๏ธ ๐Ÿ–ผ, โ†ฉ๏ธ ๐ŸŽ“ ๐ŸŽ, ๐Ÿ‘ฅ โœ”๏ธ โš™๏ธ response_model ๐Ÿ”ข. โœ‹๏ธ ๐Ÿ‘ˆ โ›“ ๐Ÿ‘ˆ ๐Ÿ‘ฅ ๐Ÿšซ ๐Ÿคš ๐Ÿ•โ€๐Ÿฆบ โšช๏ธโžก๏ธ ๐Ÿ‘จโ€๐ŸŽจ & ๐Ÿงฐ โœ… ๐Ÿ”ข ๐Ÿ“จ ๐Ÿ†Ž.

โœ‹๏ธ ๐ŸŒ… ๐Ÿ’ผ ๐ŸŒโ” ๐Ÿ‘ฅ ๐Ÿ’ช ๐Ÿ•ณ ๐Ÿ’– ๐Ÿ‘‰, ๐Ÿ‘ฅ ๐Ÿ’š ๐Ÿท โ›ฝ/โŽ ๐Ÿ“Š ๐Ÿ‘‰ ๐Ÿ–ผ.

& ๐Ÿ‘ˆ ๐Ÿ’ผ, ๐Ÿ‘ฅ ๐Ÿ’ช โš™๏ธ ๐ŸŽ“ & ๐Ÿงฌ โœŠ ๐Ÿ“ˆ ๐Ÿ”ข ๐Ÿ†Ž โœ ๐Ÿคš ๐Ÿ‘ ๐Ÿ•โ€๐Ÿฆบ ๐Ÿ‘จโ€๐ŸŽจ & ๐Ÿงฐ, & ๐Ÿคš FastAPI ๐Ÿ’ฝ ๐Ÿ–ฅ.

from typing import Union

from fastapi import FastAPI
from pydantic import BaseModel, EmailStr

app = FastAPI()


class BaseUser(BaseModel):
    username: str
    email: EmailStr
    full_name: Union[str, None] = None


class UserIn(BaseUser):
    password: str


@app.post("/user/")
async def create_user(user: UserIn) -> BaseUser:
    return user
from fastapi import FastAPI
from pydantic import BaseModel, EmailStr

app = FastAPI()


class BaseUser(BaseModel):
    username: str
    email: EmailStr
    full_name: str | None = None


class UserIn(BaseUser):
    password: str


@app.post("/user/")
async def create_user(user: UserIn) -> BaseUser:
    return user

โฎ๏ธ ๐Ÿ‘‰, ๐Ÿ‘ฅ ๐Ÿคš ๐Ÿญ ๐Ÿ•โ€๐Ÿฆบ, โšช๏ธโžก๏ธ ๐Ÿ‘จโ€๐ŸŽจ & โœ ๐Ÿ‘‰ ๐Ÿ“Ÿ โ˜‘ โš– ๐Ÿ†Ž, โœ‹๏ธ ๐Ÿ‘ฅ ๐Ÿคš ๐Ÿ’ฝ ๐Ÿ–ฅ โšช๏ธโžก๏ธ FastAPI.

โ” ๐Ÿ”จ ๐Ÿ‘‰ ๐Ÿ‘ท โ“ โžก๏ธ โœ… ๐Ÿ‘ˆ ๐Ÿ‘…. ๐Ÿ‘ถ

๐Ÿ†Ž โœ & ๐Ÿญ

๐Ÿฅ‡ โžก๏ธ ๐Ÿ‘€ โ” ๐Ÿ‘จโ€๐ŸŽจ, โœ & ๐ŸŽ ๐Ÿงฐ ๐Ÿ”œ ๐Ÿ‘€ ๐Ÿ‘‰.

BaseUser โœ”๏ธ ๐Ÿงข ๐Ÿ‘. โคด๏ธ UserIn ๐Ÿ˜– โšช๏ธโžก๏ธ BaseUser & ๐Ÿšฎ password ๐Ÿ‘,, โšซ๏ธ ๐Ÿ”œ ๐Ÿ”Œ ๐ŸŒ ๐Ÿ‘ โšช๏ธโžก๏ธ ๐Ÿ‘ฏโ€โ™‚๏ธ ๐Ÿท.

๐Ÿ‘ฅ โœ ๐Ÿ”ข ๐Ÿ“จ ๐Ÿ†Ž BaseUser, โœ‹๏ธ ๐Ÿ‘ฅ ๐Ÿค™ ๐Ÿ›ฌ UserIn ๐Ÿ‘.

๐Ÿ‘จโ€๐ŸŽจ, โœ, & ๐ŸŽ ๐Ÿงฐ ๐Ÿ† ๐Ÿšซ ๐Ÿ˜ญ ๐Ÿ”ƒ ๐Ÿ‘‰ โ†ฉ๏ธ, โŒจ โš–, UserIn ๐Ÿฟ BaseUser, โ” โ›“ โšซ๏ธ โ˜‘ ๐Ÿ†Ž ๐Ÿ•โ” โšซ๏ธโ” โŒ› ๐Ÿ•ณ ๐Ÿ‘ˆ BaseUser.

FastAPI ๐Ÿ’ฝ ๐Ÿ–ฅ

๐Ÿ”œ, FastAPI, โšซ๏ธ ๐Ÿ”œ ๐Ÿ‘€ ๐Ÿ“จ ๐Ÿ†Ž & โš’ ๐Ÿ’ญ ๐Ÿ‘ˆ โšซ๏ธโ” ๐Ÿ‘† ๐Ÿ“จ ๐Ÿ”Œ ๐Ÿ•ด ๐Ÿ‘ ๐Ÿ‘ˆ ๐Ÿ“ฃ ๐Ÿ†Ž.

FastAPI ๐Ÿ”จ ๐Ÿ“š ๐Ÿ‘œ ๐Ÿ”˜ โฎ๏ธ Pydantic โš’ ๐Ÿ’ญ ๐Ÿ‘ˆ ๐Ÿ“š ๐ŸŽ ๐Ÿšซ ๐ŸŽ“ ๐Ÿงฌ ๐Ÿšซ โš™๏ธ ๐Ÿ“จ ๐Ÿ’ฝ ๐Ÿ–ฅ, โช ๐Ÿ‘† ๐Ÿ’ช ๐Ÿ”š ๐Ÿ†™ ๐Ÿ›ฌ ๐ŸŒ… ๐ŸŒ… ๐Ÿ’ฝ ๐ŸŒ˜ โšซ๏ธโ” ๐Ÿ‘† ๐Ÿ“ˆ.

๐Ÿ‘‰ ๐ŸŒŒ, ๐Ÿ‘† ๐Ÿ’ช ๐Ÿคš ๐Ÿ† ๐Ÿ‘ฏโ€โ™‚๏ธ ๐ŸŒ: ๐Ÿ†Ž โœ โฎ๏ธ ๐Ÿญ ๐Ÿ•โ€๐Ÿฆบ & ๐Ÿ’ฝ ๐Ÿ–ฅ.

๐Ÿ‘€ โšซ๏ธ ๐Ÿฉบ

๐Ÿ•โ” ๐Ÿ‘† ๐Ÿ‘€ ๐Ÿง ๐Ÿฉบ, ๐Ÿ‘† ๐Ÿ’ช โœ… ๐Ÿ‘ˆ ๐Ÿ”ข ๐Ÿท & ๐Ÿ”ข ๐Ÿท ๐Ÿ”œ ๐Ÿ‘ฏโ€โ™‚๏ธ โœ”๏ธ ๐Ÿ‘ซ ๐Ÿ‘ ๐ŸŽป ๐Ÿ”—:

& ๐Ÿ‘ฏโ€โ™‚๏ธ ๐Ÿท ๐Ÿ”œ โš™๏ธ ๐ŸŽ“ ๐Ÿ› ๏ธ ๐Ÿงพ:

๐ŸŽ ๐Ÿ“จ ๐Ÿ†Ž โœ

๐Ÿ“ค 5๏ธโƒฃ๐Ÿ“† ๐Ÿ’ผ ๐ŸŒโ” ๐Ÿ‘† ๐Ÿ“จ ๐Ÿ•ณ ๐Ÿ‘ˆ ๐Ÿšซ โ˜‘ Pydantic ๐Ÿ‘ & ๐Ÿ‘† โœ โšซ๏ธ ๐Ÿ”ข, ๐Ÿ•ด ๐Ÿคš ๐Ÿ•โ€๐Ÿฆบ ๐Ÿšš ๐Ÿญ (๐Ÿ‘จโ€๐ŸŽจ, โœ, โ™’๏ธ).

๐Ÿ“จ ๐Ÿ“จ ๐Ÿ”—

๐Ÿ† โš  ๐Ÿ’ผ ๐Ÿ”œ ๐Ÿ›ฌ ๐Ÿ“จ ๐Ÿ”— ๐Ÿ”ฌ โช ๐Ÿง ๐Ÿฉบ.

from fastapi import FastAPI, Response
from fastapi.responses import JSONResponse, RedirectResponse

app = FastAPI()


@app.get("/portal")
async def get_portal(teleport: bool = False) -> Response:
    if teleport:
        return RedirectResponse(url="https://www.youtube.com/watch?v=dQw4w9WgXcQ")
    return JSONResponse(content={"message": "Here's your interdimensional portal."})

๐Ÿ‘‰ ๐Ÿ™… ๐Ÿ’ผ ๐Ÿต ๐Ÿ” FastAPI โ†ฉ๏ธ ๐Ÿ“จ ๐Ÿ†Ž โœ ๐ŸŽ“ (โš–๏ธ ๐Ÿฟ) Response.

& ๐Ÿงฐ ๐Ÿ”œ ๐Ÿ˜„ โ†ฉ๏ธ ๐Ÿ‘ฏโ€โ™‚๏ธ RedirectResponse & JSONResponse ๐Ÿฟ Response, ๐Ÿ†Ž โœ โ˜‘.

โœ ๐Ÿ“จ ๐Ÿฟ

๐Ÿ‘† ๐Ÿ’ช โš™๏ธ ๐Ÿฟ Response ๐Ÿ†Ž โœ:

from fastapi import FastAPI
from fastapi.responses import RedirectResponse

app = FastAPI()


@app.get("/teleport")
async def get_teleport() -> RedirectResponse:
    return RedirectResponse(url="https://www.youtube.com/watch?v=dQw4w9WgXcQ")

๐Ÿ‘‰ ๐Ÿ”œ ๐Ÿ‘ท โ†ฉ๏ธ RedirectResponse ๐Ÿฟ Response, & FastAPI ๐Ÿ”œ ๐Ÿ” ๐Ÿต ๐Ÿ‘‰ ๐Ÿ™… ๐Ÿ’ผ.

โŒ ๐Ÿ“จ ๐Ÿ†Ž โœ

โœ‹๏ธ ๐Ÿ•โ” ๐Ÿ‘† ๐Ÿ“จ ๐ŸŽ โŒ ๐ŸŽš ๐Ÿ‘ˆ ๐Ÿšซ โ˜‘ Pydantic ๐Ÿ†Ž (โœ… ๐Ÿ’ฝ ๐ŸŽš) & ๐Ÿ‘† โœ โšซ๏ธ ๐Ÿ’– ๐Ÿ‘ˆ ๐Ÿ”ข, FastAPI ๐Ÿ”œ ๐Ÿ”„ โœ Pydantic ๐Ÿ“จ ๐Ÿท โšช๏ธโžก๏ธ ๐Ÿ‘ˆ ๐Ÿ†Ž โœ, & ๐Ÿ”œ โŒ.

๐ŸŽ ๐Ÿ”œ ๐Ÿ”จ ๐Ÿšฅ ๐Ÿ‘† โœ”๏ธ ๐Ÿ•ณ ๐Ÿ’– ๐Ÿ‡ช๐Ÿ‡บ ๐Ÿ–– ๐ŸŽ ๐Ÿ†Ž ๐ŸŒโ” 1๏ธโƒฃ โš–๏ธ ๐ŸŒ… ๐Ÿ‘ซ ๐Ÿšซ โ˜‘ Pydantic ๐Ÿ†Ž, ๐Ÿ–ผ ๐Ÿ‘‰ ๐Ÿ”œ โŒ ๐Ÿ‘ถ:

from typing import Union

from fastapi import FastAPI, Response
from fastapi.responses import RedirectResponse

app = FastAPI()


@app.get("/portal")
async def get_portal(teleport: bool = False) -> Union[Response, dict]:
    if teleport:
        return RedirectResponse(url="https://www.youtube.com/watch?v=dQw4w9WgXcQ")
    return {"message": "Here's your interdimensional portal."}
from fastapi import FastAPI, Response
from fastapi.responses import RedirectResponse

app = FastAPI()


@app.get("/portal")
async def get_portal(teleport: bool = False) -> Response | dict:
    if teleport:
        return RedirectResponse(url="https://www.youtube.com/watch?v=dQw4w9WgXcQ")
    return {"message": "Here's your interdimensional portal."}

...๐Ÿ‘‰ โŒ โ†ฉ๏ธ ๐Ÿ†Ž โœ ๐Ÿšซ Pydantic ๐Ÿ†Ž & ๐Ÿšซ ๐Ÿ‘ Response ๐ŸŽ“ โš–๏ธ ๐Ÿฟ, โšซ๏ธ ๐Ÿ‡ช๐Ÿ‡บ (๐Ÿ™† 2๏ธโƒฃ) ๐Ÿ–– Response & dict.

โŽ ๐Ÿ“จ ๐Ÿท

โ–ถ๏ธ โšช๏ธโžก๏ธ ๐Ÿ–ผ ๐Ÿ”›, ๐Ÿ‘† 5๏ธโƒฃ๐Ÿ“† ๐Ÿšซ ๐Ÿ’š โœ”๏ธ ๐Ÿ”ข ๐Ÿ’ฝ ๐Ÿ”ฌ, ๐Ÿงพ, ๐Ÿ–ฅ, โ™’๏ธ. ๐Ÿ‘ˆ ๐ŸŽญ FastAPI.

โœ‹๏ธ ๐Ÿ‘† ๐Ÿ’ช ๐Ÿ’š ๐Ÿšง ๐Ÿ“จ ๐Ÿ†Ž โœ ๐Ÿ”ข ๐Ÿคš ๐Ÿ•โ€๐Ÿฆบ โšช๏ธโžก๏ธ ๐Ÿงฐ ๐Ÿ’– ๐Ÿ‘จโ€๐ŸŽจ & ๐Ÿ†Ž โ˜‘ (โœ… โœ).

๐Ÿ‘‰ ๐Ÿ’ผ, ๐Ÿ‘† ๐Ÿ’ช โŽ ๐Ÿ“จ ๐Ÿท โšก โš’ response_model=None:

from typing import Union

from fastapi import FastAPI, Response
from fastapi.responses import RedirectResponse

app = FastAPI()


@app.get("/portal", response_model=None)
async def get_portal(teleport: bool = False) -> Union[Response, dict]:
    if teleport:
        return RedirectResponse(url="https://www.youtube.com/watch?v=dQw4w9WgXcQ")
    return {"message": "Here's your interdimensional portal."}
from fastapi import FastAPI, Response
from fastapi.responses import RedirectResponse

app = FastAPI()


@app.get("/portal", response_model=None)
async def get_portal(teleport: bool = False) -> Response | dict:
    if teleport:
        return RedirectResponse(url="https://www.youtube.com/watch?v=dQw4w9WgXcQ")
    return {"message": "Here's your interdimensional portal."}

๐Ÿ‘‰ ๐Ÿ”œ โš’ FastAPI ๐Ÿšถ ๐Ÿ“จ ๐Ÿท โšก & ๐Ÿ‘ˆ ๐ŸŒŒ ๐Ÿ‘† ๐Ÿ’ช โœ”๏ธ ๐Ÿ™† ๐Ÿ“จ ๐Ÿ†Ž โœ ๐Ÿ‘† ๐Ÿ’ช ๐Ÿต โšซ๏ธ ๐Ÿค• ๐Ÿ‘† FastAPI ๐Ÿˆธ. ๐Ÿ‘ถ

๐Ÿ“จ ๐Ÿท ๐Ÿ”ข ๐Ÿ”ข

๐Ÿ‘† ๐Ÿ“จ ๐Ÿท ๐Ÿ’ช โœ”๏ธ ๐Ÿ”ข ๐Ÿ’ฒ, ๐Ÿ’–:

from typing import List, Union

from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()


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


items = {
    "foo": {"name": "Foo", "price": 50.2},
    "bar": {"name": "Bar", "description": "The bartenders", "price": 62, "tax": 20.2},
    "baz": {"name": "Baz", "description": None, "price": 50.2, "tax": 10.5, "tags": []},
}


@app.get("/items/{item_id}", response_model=Item, response_model_exclude_unset=True)
async def read_item(item_id: str):
    return items[item_id]
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: float = 10.5
    tags: list[str] = []


items = {
    "foo": {"name": "Foo", "price": 50.2},
    "bar": {"name": "Bar", "description": "The bartenders", "price": 62, "tax": 20.2},
    "baz": {"name": "Baz", "description": None, "price": 50.2, "tax": 10.5, "tags": []},
}


@app.get("/items/{item_id}", response_model=Item, response_model_exclude_unset=True)
async def read_item(item_id: str):
    return items[item_id]
from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()


class Item(BaseModel):
    name: str
    description: str | None = None
    price: float
    tax: float = 10.5
    tags: list[str] = []


items = {
    "foo": {"name": "Foo", "price": 50.2},
    "bar": {"name": "Bar", "description": "The bartenders", "price": 62, "tax": 20.2},
    "baz": {"name": "Baz", "description": None, "price": 50.2, "tax": 10.5, "tags": []},
}


@app.get("/items/{item_id}", response_model=Item, response_model_exclude_unset=True)
async def read_item(item_id: str):
    return items[item_id]
  • description: Union[str, None] = None (โš–๏ธ str | None = None ๐Ÿ 3๏ธโƒฃ.1๏ธโƒฃ0๏ธโƒฃ) โœ”๏ธ ๐Ÿ”ข None.
  • tax: float = 10.5 โœ”๏ธ ๐Ÿ”ข 10.5.
  • tags: List[str] = [] ๐Ÿ”ข ๐Ÿ› ๐Ÿ“‡: [].

โœ‹๏ธ ๐Ÿ‘† ๐Ÿ’ช ๐Ÿ’š ๐Ÿšซ ๐Ÿ‘ซ โšช๏ธโžก๏ธ ๐Ÿ ๐Ÿšฅ ๐Ÿ‘ซ ๐Ÿšซ ๐Ÿค™ ๐Ÿช.

๐Ÿ–ผ, ๐Ÿšฅ ๐Ÿ‘† โœ”๏ธ ๐Ÿท โฎ๏ธ ๐Ÿ“š ๐Ÿ“ฆ ๐Ÿ”ข โ˜ ๐Ÿ’ฝ, โœ‹๏ธ ๐Ÿ‘† ๐Ÿšซ ๐Ÿ’š ๐Ÿ“จ ๐Ÿ“ถ ๐Ÿ“ ๐ŸŽป ๐Ÿ“จ ๐ŸŒ• ๐Ÿ”ข ๐Ÿ’ฒ.

โš™๏ธ response_model_exclude_unset ๐Ÿ”ข

๐Ÿ‘† ๐Ÿ’ช โš’ โžก ๐Ÿ› ๏ธ ๐Ÿ‘จโ€๐ŸŽจ ๐Ÿ”ข response_model_exclude_unset=True:

from typing import List, Union

from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()


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


items = {
    "foo": {"name": "Foo", "price": 50.2},
    "bar": {"name": "Bar", "description": "The bartenders", "price": 62, "tax": 20.2},
    "baz": {"name": "Baz", "description": None, "price": 50.2, "tax": 10.5, "tags": []},
}


@app.get("/items/{item_id}", response_model=Item, response_model_exclude_unset=True)
async def read_item(item_id: str):
    return items[item_id]
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: float = 10.5
    tags: list[str] = []


items = {
    "foo": {"name": "Foo", "price": 50.2},
    "bar": {"name": "Bar", "description": "The bartenders", "price": 62, "tax": 20.2},
    "baz": {"name": "Baz", "description": None, "price": 50.2, "tax": 10.5, "tags": []},
}


@app.get("/items/{item_id}", response_model=Item, response_model_exclude_unset=True)
async def read_item(item_id: str):
    return items[item_id]
from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()


class Item(BaseModel):
    name: str
    description: str | None = None
    price: float
    tax: float = 10.5
    tags: list[str] = []


items = {
    "foo": {"name": "Foo", "price": 50.2},
    "bar": {"name": "Bar", "description": "The bartenders", "price": 62, "tax": 20.2},
    "baz": {"name": "Baz", "description": None, "price": 50.2, "tax": 10.5, "tags": []},
}


@app.get("/items/{item_id}", response_model=Item, response_model_exclude_unset=True)
async def read_item(item_id: str):
    return items[item_id]

& ๐Ÿ‘ˆ ๐Ÿ”ข ๐Ÿ’ฒ ๐Ÿ† ๐Ÿšซ ๐Ÿ”Œ ๐Ÿ“จ, ๐Ÿ•ด ๐Ÿ’ฒ ๐Ÿค™ โš’.

, ๐Ÿšฅ ๐Ÿ‘† ๐Ÿ“จ ๐Ÿ“จ ๐Ÿ‘ˆ โžก ๐Ÿ› ๏ธ ๐Ÿฌ โฎ๏ธ ๐Ÿ†” foo, ๐Ÿ“จ (๐Ÿšซ โœ… ๐Ÿ”ข ๐Ÿ’ฒ) ๐Ÿ”œ:

{
    "name": "Foo",
    "price": 50.2
}

Info

FastAPI โš™๏ธ Pydantic ๐Ÿท .dict() โฎ๏ธ ๐Ÿšฎ exclude_unset ๐Ÿ”ข ๐Ÿ† ๐Ÿ‘‰.

Info

๐Ÿ‘† ๐Ÿ’ช โš™๏ธ:

  • response_model_exclude_defaults=True
  • response_model_exclude_none=True

๐Ÿ”ฌ Pydantic ๐Ÿฉบ exclude_defaults & exclude_none.

๐Ÿ“Š โฎ๏ธ ๐Ÿ’ฒ ๐Ÿ‘ โฎ๏ธ ๐Ÿ”ข

โœ‹๏ธ ๐Ÿšฅ ๐Ÿ‘† ๐Ÿ“Š โœ”๏ธ ๐Ÿ’ฒ ๐Ÿท ๐Ÿ‘ โฎ๏ธ ๐Ÿ”ข ๐Ÿ’ฒ, ๐Ÿ’– ๐Ÿฌ โฎ๏ธ ๐Ÿ†” bar:

{
    "name": "Bar",
    "description": "The bartenders",
    "price": 62,
    "tax": 20.2
}

๐Ÿ‘ซ ๐Ÿ”œ ๐Ÿ”Œ ๐Ÿ“จ.

๐Ÿ“Š โฎ๏ธ ๐ŸŽ ๐Ÿ’ฒ ๐Ÿ”ข

๐Ÿšฅ ๐Ÿ“Š โœ”๏ธ ๐ŸŽ ๐Ÿ’ฒ ๐Ÿ”ข ๐Ÿ•, ๐Ÿ’– ๐Ÿฌ โฎ๏ธ ๐Ÿ†” baz:

{
    "name": "Baz",
    "description": None,
    "price": 50.2,
    "tax": 10.5,
    "tags": []
}

FastAPI ๐Ÿ™ƒ ๐Ÿฅƒ (๐Ÿค™, Pydantic ๐Ÿ™ƒ ๐Ÿฅƒ) ๐Ÿค” ๐Ÿ‘ˆ, โœ‹๏ธ description, tax, & tags โœ”๏ธ ๐ŸŽ ๐Ÿ’ฒ ๐Ÿ”ข, ๐Ÿ‘ซ โš’ ๐ŸŽฏ (โ†ฉ๏ธ โœŠ โšช๏ธโžก๏ธ ๐Ÿ”ข).

, ๐Ÿ‘ซ ๐Ÿ”œ ๐Ÿ”Œ ๐ŸŽป ๐Ÿ“จ.

Tip

๐Ÿ‘€ ๐Ÿ‘ˆ ๐Ÿ”ข ๐Ÿ’ฒ ๐Ÿ’ช ๐Ÿ•ณ, ๐Ÿšซ ๐Ÿ•ด None.

๐Ÿ‘ซ ๐Ÿ’ช ๐Ÿ“‡ ([]), float 10.5, โ™’๏ธ.

response_model_include & response_model_exclude

๐Ÿ‘† ๐Ÿ’ช โš™๏ธ โžก ๐Ÿ› ๏ธ ๐Ÿ‘จโ€๐ŸŽจ ๐Ÿ”ข response_model_include & response_model_exclude.

๐Ÿ‘ซ โœŠ set str โฎ๏ธ ๐Ÿ“› ๐Ÿ”ข ๐Ÿ”Œ (โŽ ๐ŸŽ‚) โš–๏ธ ๐Ÿšซ (โœ… ๐ŸŽ‚).

๐Ÿ‘‰ ๐Ÿ’ช โš™๏ธ โฉ โŒจ ๐Ÿšฅ ๐Ÿ‘† โœ”๏ธ ๐Ÿ•ด 1๏ธโƒฃ Pydantic ๐Ÿท & ๐Ÿ’š โŽ ๐Ÿ’ฝ โšช๏ธโžก๏ธ ๐Ÿ”ข.

Tip

โœ‹๏ธ โšซ๏ธ ๐Ÿ‘ โš™๏ธ ๐Ÿ’ญ ๐Ÿ”›, โš™๏ธ ๐Ÿ’— ๐ŸŽ“, โ†ฉ๏ธ ๐Ÿ‘ซ ๐Ÿ”ข.

๐Ÿ‘‰ โ†ฉ๏ธ ๐ŸŽป ๐Ÿ”— ๐Ÿ— ๐Ÿ‘† ๐Ÿ“ฑ ๐Ÿ—„ (& ๐Ÿฉบ) ๐Ÿ”œ 1๏ธโƒฃ ๐Ÿ ๐Ÿท, ๐Ÿšฅ ๐Ÿ‘† โš™๏ธ response_model_include โš–๏ธ response_model_exclude ๐Ÿšซ ๐Ÿ”ข.

๐Ÿ‘‰ โœ” response_model_by_alias ๐Ÿ‘ˆ ๐Ÿ‘ท โžก.

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: float = 10.5


items = {
    "foo": {"name": "Foo", "price": 50.2},
    "bar": {"name": "Bar", "description": "The Bar fighters", "price": 62, "tax": 20.2},
    "baz": {
        "name": "Baz",
        "description": "There goes my baz",
        "price": 50.2,
        "tax": 10.5,
    },
}


@app.get(
    "/items/{item_id}/name",
    response_model=Item,
    response_model_include={"name", "description"},
)
async def read_item_name(item_id: str):
    return items[item_id]


@app.get("/items/{item_id}/public", response_model=Item, response_model_exclude={"tax"})
async def read_item_public_data(item_id: str):
    return items[item_id]
from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()


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


items = {
    "foo": {"name": "Foo", "price": 50.2},
    "bar": {"name": "Bar", "description": "The Bar fighters", "price": 62, "tax": 20.2},
    "baz": {
        "name": "Baz",
        "description": "There goes my baz",
        "price": 50.2,
        "tax": 10.5,
    },
}


@app.get(
    "/items/{item_id}/name",
    response_model=Item,
    response_model_include={"name", "description"},
)
async def read_item_name(item_id: str):
    return items[item_id]


@app.get("/items/{item_id}/public", response_model=Item, response_model_exclude={"tax"})
async def read_item_public_data(item_id: str):
    return items[item_id]

Tip

โ• {"name", "description"} โœ set โฎ๏ธ ๐Ÿ“š 2๏ธโƒฃ ๐Ÿ’ฒ.

โšซ๏ธ ๐ŸŒ“ set(["name", "description"]).

โš™๏ธ listโ“‚ โ†ฉ๏ธ setโ“‚

๐Ÿšฅ ๐Ÿ‘† ๐Ÿ’ญ โš™๏ธ set & โš™๏ธ list โš–๏ธ tuple โ†ฉ๏ธ, FastAPI ๐Ÿ”œ ๐Ÿ—œ โšซ๏ธ set & โšซ๏ธ ๐Ÿ”œ ๐Ÿ‘ท โ˜‘:

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: float = 10.5


items = {
    "foo": {"name": "Foo", "price": 50.2},
    "bar": {"name": "Bar", "description": "The Bar fighters", "price": 62, "tax": 20.2},
    "baz": {
        "name": "Baz",
        "description": "There goes my baz",
        "price": 50.2,
        "tax": 10.5,
    },
}


@app.get(
    "/items/{item_id}/name",
    response_model=Item,
    response_model_include=["name", "description"],
)
async def read_item_name(item_id: str):
    return items[item_id]


@app.get("/items/{item_id}/public", response_model=Item, response_model_exclude=["tax"])
async def read_item_public_data(item_id: str):
    return items[item_id]
from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()


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


items = {
    "foo": {"name": "Foo", "price": 50.2},
    "bar": {"name": "Bar", "description": "The Bar fighters", "price": 62, "tax": 20.2},
    "baz": {
        "name": "Baz",
        "description": "There goes my baz",
        "price": 50.2,
        "tax": 10.5,
    },
}


@app.get(
    "/items/{item_id}/name",
    response_model=Item,
    response_model_include=["name", "description"],
)
async def read_item_name(item_id: str):
    return items[item_id]


@app.get("/items/{item_id}/public", response_model=Item, response_model_exclude=["tax"])
async def read_item_public_data(item_id: str):
    return items[item_id]

๐ŸŒƒ

โš™๏ธ โžก ๐Ÿ› ๏ธ ๐Ÿ‘จโ€๐ŸŽจ ๐Ÿ”ข response_model ๐Ÿ”ฌ ๐Ÿ“จ ๐Ÿท & โœด๏ธ ๐Ÿšš ๐Ÿ“ข ๐Ÿ’ฝ โ›ฝ ๐Ÿ‘….

โš™๏ธ response_model_exclude_unset ๐Ÿ“จ ๐Ÿ•ด ๐Ÿ’ฒ ๐ŸŽฏ โš’.