๐จ ๐ท - ๐จ ๐¶
๐ ๐ช ๐ฃ ๐ โ๏ธ ๐จ โ โก ๐ ๏ธ ๐ข ๐จ ๐.
๐ ๐ช โ๏ธ ๐ โ ๐ ๐ ๐ ๐ ๐ข ๐ฝ ๐ข ๐ข, ๐ ๐ช โ๏ธ 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
๐จ ๐ด ๐ฒ ๐ฏ โ.