国产chinesehdxxxx野外,国产av无码专区亚洲av琪琪,播放男人添女人下边视频,成人国产精品一区二区免费看,chinese丰满人妻videos

FastAPI教程 路徑參數和數值校驗

2022-08-20 11:33 更新

與使用 Query 為查詢參數聲明更多的校驗和元數據的方式相同,你也可以使用 Path 為路徑參數聲明相同類型的校驗和元數據。

導入 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

聲明元數據

你可以聲明與 Query 相同的所有參數。

例如,要聲明路徑參數 item_id的 title 元數據值,你可以輸入:

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

路徑參數總是必需的,因為它必須是路徑的一部分。

所以,你應該在聲明時使用 ... 將其標記為必需參數。

然而,即使你使用 None 聲明路徑參數或設置一個其他默認值也不會有任何影響,它依然會是必需參數。

按需對參數排序

假設你想要聲明一個必需的 str 類型查詢參數 q。

而且你不需要為該參數聲明任何其他內容,所以實際上你并不需要使用 Query。

但是你仍然需要使用 Path 來聲明路徑參數 item_id。

如果你將帶有「默認值」的參數放在沒有「默認值」的參數之前,Python 將會報錯。

但是你可以對其重新排序,并將不帶默認值的值(查詢參數 q)放到最前面。

對 FastAPI 來說這無關緊要。它將通過參數的名稱、類型和默認值聲明(Query、Path 等)來檢測參數,而不在乎參數的順序。

因此,你可以將函數聲明為:

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

按需對參數排序的技巧

如果你想不使用 Query 聲明沒有默認值的查詢參數 q,同時使用 Path 聲明路徑參數 item_id,并使它們的順序與上面不同,Python 對此有一些特殊的語法。

傳遞 * 作為函數的第一個參數。

Python 不會對該 * 做任何事情,但是它將知道之后的所有參數都應作為關鍵字參數(鍵值對),也被稱為 kwargs,來調用。即使它們沒有默認值。

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

數值校驗:大于等于

使用 Query 和 Path(以及你將在后面看到的其他類)可以聲明字符串約束,但也可以聲明數值約束。

像下面這樣,添加 ge=1 后,item_id 將必須是一個大于(greater than)或等于(equal)1 的整數。

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

數值校驗:大于和小于等于

同樣的規(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

數值校驗:浮點數、大于和小于

數值校驗同樣適用于 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

總結

你能夠以與 查詢參數和字符串校驗 相同的方式使用 Query、Path(以及其他你還沒見過的類)聲明元數據和字符串校驗。

而且你還可以聲明數值校驗:

  • gt:大于(greater than)
  • ge:大于等于(greater than or equal)
  • lt:小于(less than)
  • le:小于等于(less than or equal)

Info

Query、Path 以及你后面會看到的其他類繼承自一個共同的 Param 類(不需要直接使用它)。

而且它們都共享相同的所有你已看到并用于添加額外校驗和元數據的參數。

技術細節(jié)

當你從 fastapi 導入 Query、Path 和其他同類對象時,它們實際上是函數。

當被調用時,它們返回同名類的實例。

如此,你導入 Query 這個函數。當你調用它時,它將返回一個同樣命名為 Query 的類的實例。

因為使用了這些函數(而不是直接使用類),所以你的編輯器不會標記有關其類型的錯誤。

這樣,你可以使用常規(guī)的編輯器和編碼工具,而不必添加自定義配置來忽略這些錯誤。


以上內容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號