新聞中心
與使用 Query 為查詢參數(shù)聲明更多的校驗和元數(shù)據(jù)的方式相同,你也可以使用 Path 為路徑參數(shù)聲明相同類型的校驗和元數(shù)據(jù)。

導入 Path
首先,從 fastapi 導入 Path:
from typing import Optional
from fastapi import FastAPI, Path, Query
app = FastAPI()
@app.get("/items/{item_id}")
async def read_items(
item_id: int = Path(..., title="The ID of the item to get"),
q: Optional[str] = Query(None, alias="item-query"),
):
results = {"item_id": item_id}
if q:
results.update({"q": q})
return results
聲明元數(shù)據(jù)
你可以聲明與 Query 相同的所有參數(shù)。
例如,要聲明路徑參數(shù) item_id的 title 元數(shù)據(jù)值,你可以輸入:
from typing import Optional
from fastapi import FastAPI, Path, Query
app = FastAPI()
@app.get("/items/{item_id}")
async def read_items(
item_id: int = Path(..., title="The ID of the item to get"),
q: Optional[str] = Query(None, alias="item-query"),
):
results = {"item_id": item_id}
if q:
results.update({"q": q})
return results
Note
路徑參數(shù)總是必需的,因為它必須是路徑的一部分。
所以,你應該在聲明時使用 ... 將其標記為必需參數(shù)。
然而,即使你使用 None 聲明路徑參數(shù)或設置一個其他默認值也不會有任何影響,它依然會是必需參數(shù)。
按需對參數(shù)排序
假設你想要聲明一個必需的 str 類型查詢參數(shù) q。
而且你不需要為該參數(shù)聲明任何其他內(nèi)容,所以實際上你并不需要使用 Query。
但是你仍然需要使用 Path 來聲明路徑參數(shù) item_id。
如果你將帶有「默認值」的參數(shù)放在沒有「默認值」的參數(shù)之前,Python 將會報錯。
但是你可以對其重新排序,并將不帶默認值的值(查詢參數(shù) q)放到最前面。
對 FastAPI 來說這無關緊要。它將通過參數(shù)的名稱、類型和默認值聲明(Query、Path 等)來檢測參數(shù),而不在乎參數(shù)的順序。
因此,你可以將函數(shù)聲明為:
from fastapi import FastAPI, Path
app = FastAPI()
@app.get("/items/{item_id}")
async def read_items(
q: str, item_id: int = Path(..., title="The ID of the item to get")
):
results = {"item_id": item_id}
if q:
results.update({"q": q})
return results
按需對參數(shù)排序的技巧
如果你想不使用 Query 聲明沒有默認值的查詢參數(shù) q,同時使用 Path 聲明路徑參數(shù) item_id,并使它們的順序與上面不同,Python 對此有一些特殊的語法。
傳遞 * 作為函數(shù)的第一個參數(shù)。
Python 不會對該 * 做任何事情,但是它將知道之后的所有參數(shù)都應作為關鍵字參數(shù)(鍵值對),也被稱為 kwargs,來調(diào)用。即使它們沒有默認值。
from fastapi import FastAPI, Path
app = FastAPI()
@app.get("/items/{item_id}")
async def read_items(
*, item_id: int = Path(..., title="The ID of the item to get"), q: str
):
results = {"item_id": item_id}
if q:
results.update({"q": q})
return results
數(shù)值校驗:大于等于
使用 Query 和 Path(以及你將在后面看到的其他類)可以聲明字符串約束,但也可以聲明數(shù)值約束。
像下面這樣,添加 ge=1 后,item_id 將必須是一個大于(greater than)或等于(equal)1 的整數(shù)。
from fastapi import FastAPI, Path
app = FastAPI()
@app.get("/items/{item_id}")
async def read_items(
*, item_id: int = Path(..., title="The ID of the item to get", ge=1), q: str
):
results = {"item_id": item_id}
if q:
results.update({"q": q})
return results
數(shù)值校驗:大于和小于等于
同樣的規(guī)則適用于:
- gt:大于(greater than)
- le:小于等于(less than or equal)
from fastapi import FastAPI, Path
app = FastAPI()
@app.get("/items/{item_id}")
async def read_items(
*,
item_id: int = Path(..., title="The ID of the item to get", gt=0, le=1000),
q: str,
):
results = {"item_id": item_id}
if q:
results.update({"q": q})
return results
數(shù)值校驗:浮點數(shù)、大于和小于
數(shù)值校驗同樣適用于 float 值。
能夠聲明 gt 而不僅僅是 ge 在這個前提下變得重要起來。例如,你可以要求一個值必須大于 0,即使它小于 1。
因此,0.5 將是有效值。但是 0.0或 0 不是。
對于 lt 也是一樣的。
from fastapi import FastAPI, Path, Query
app = FastAPI()
@app.get("/items/{item_id}")
async def read_items(
*,
item_id: int = Path(..., title="The ID of the item to get", ge=0, le=1000),
q: str,
size: float = Query(..., gt=0, lt=10.5)
):
results = {"item_id": item_id}
if q:
results.update({"q": q})
return results
總結(jié)
你能夠以與 查詢參數(shù)和字符串校驗 相同的方式使用 Query、Path(以及其他你還沒見過的類)聲明元數(shù)據(jù)和字符串校驗。
而且你還可以聲明數(shù)值校驗:
- gt:大于(greater than)
- ge:大于等于(greater than or equal)
- lt:小于(less than)
- le:小于等于(less than or equal)
Info
Query、Path 以及你后面會看到的其他類繼承自一個共同的 Param 類(不需要直接使用它)。
而且它們都共享相同的所有你已看到并用于添加額外校驗和元數(shù)據(jù)的參數(shù)。
技術(shù)細節(jié)
當你從 fastapi 導入 Query、Path 和其他同類對象時,它們實際上是函數(shù)。
當被調(diào)用時,它們返回同名類的實例。
如此,你導入 Query 這個函數(shù)。當你調(diào)用它時,它將返回一個同樣命名為 Query 的類的實例。
因為使用了這些函數(shù)(而不是直接使用類),所以你的編輯器不會標記有關其類型的錯誤。
這樣,你可以使用常規(guī)的編輯器和編碼工具,而不必添加自定義配置來忽略這些錯誤。
網(wǎng)站題目:創(chuàng)新互聯(lián)FastAPI教程:FastAPI教程 路徑參數(shù)和數(shù)值校驗
標題鏈接:http://m.fisionsoft.com.cn/article/dhisipp.html


咨詢
建站咨詢
