๐ช - ๐ ๐ข¶
๐ ๐ ๐ฅ โ๏ธ ๐ โ โ๏ธ 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 โฏ ๐ช ๐ ๐โ ๐ค ๐ด ๐ ๐ข ๐ฃ.