網站首頁 編程語言 正文
前言:
在fastapi中,我們定義的查詢參數是可以設置成:必選參數 or 可選參數。
可選查詢參數
- 只要給查詢參數的默認值設置為
None
,表示該查詢參數是可選參數。
?from fastapi import FastAPI ?? ?app = FastAPI()? ?@app.get("/items/{item_id}") ?async def read_item(item_id: str, q=None): ? ? ?data = {"item_id": item_id} ? ? ?if q: ? ? ? ? ?data["q"] = q ? ? ?return data
補充:此時路徑操作函數內的參數有兩個,一個是路徑參數
item_id
,一個是查詢參數q
,fastapi是可以區分他們的。
必選查詢參數
- 當你為查詢參數設置默認值時,則該參數在URL中不是必須的。
- 如果你不想添加默認值,而只是想使該參數成為可選的,則將默認值設置為?
None
。 - 但當你想讓一個查詢參數成為必需的,不聲明任何默認值就可以.
- 比如:這里的查詢參數?
needy
?是類型為?str
?的必需查詢參數。
?from fastapi import FastAPI ?? ?app = FastAPI()? ?@app.get("/items/{item_id}") ?async def read_user_item(item_id: str, needy: str): ? ? ?item = {"item_id": item_id, "needy": needy} ? ? ?return item
- 如果在URL中沒有查詢參數
needy
,則報錯
?{ ? ? "detail": [ ? ? ? ? { ? ? ? ? ? ? "loc": [ ? ? ? ? ? ? ? ? "query", ? ? ? ? ? ? ? ? "needy" ? ? ? ? ? ? ], ? ? ? ? ? ? "msg": "field required", ? ? ? ? ? ? "type": "value_error.missing" ? ? ? ? } ? ? ] ?}
可選和必選參數共存
- 也可以定義一些參數為必需的,一些具有默認值,而某些則完全是可選的
- 此時:
itme_id
是路徑參數,needy
是必選路徑參數,skip
是有默認值必選查詢參數,limit
是可選查詢參數。
?from fastapi import FastAPI ?? ?app = FastAPI() ?@app.get("/items/{item_id}") ?async def read_user_item( ? ? ?item_id: str, needy: str, skip: int = 0, limit=None ?): ? ? ?item = {"item_id": item_id, "needy": needy, "skip": skip, "limit": limit} ? ? ?return item
為可選參數做類型提示
- 比如一個可選的參數
q
,如有該參數時,他的類型是整型,此時定義類型提示如下 - 使用
typing
模塊下的Union
做類型提示,Union[int, None]
表示類型是int
或者None
- 此時對于URL:
http://127.0.0.1:8000/items/12?q=12
,參數q
就會自動轉化為數字12
?from typing import Union? ?from fastapi import FastAPI ?app = FastAPI()? ?@app.get("/items/{item_id}") ?async def read_item(item_id: str, q: Union[int, None] = None): ? ? ?data = {"item_id": item_id} ? ? ?if q: ? ? ? ? ?data["q"] = q ? ? ?return data
補充1:
- 可能為空的情況做類型提示,一般使用一個比
typing.Union
更加方便的類型:typing.Optional
- 因為,
Optional[X] is equivalent to Union[X, None]
- 所以,上面的類似等價于
q: Optional[int] = None
\
補充2:
- 只要給查詢參數默認值
None
就表示它是可選查詢參數,和類型提示無關。 - 類型提示的功能在于如果該值存在是他應該是什么類型的變量并做類型轉換和校驗。
原文鏈接:https://juejin.cn/post/7105568560461643783
相關推薦
- 2022-04-07 C++中類的默認成員函數詳解_C 語言
- 2023-01-13 Golang分布式鎖簡單案例實現流程_Golang
- 2022-10-17 QT實現TCP網絡聊天室_C 語言
- 2022-05-02 C/C++的各種字符串函數你知道幾個_C 語言
- 2022-05-31 Python學習之加密模塊使用詳解_python
- 2022-11-26 pytorch邏輯回歸實現步驟詳解_python
- 2023-03-21 C#實體類轉換的兩種方式小結_C#教程
- 2022-09-15 Python實現圖形用戶界面計算器_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支