隐藏页面特效

FastAPI系列:查询字符串参数额外校验Query

设置默认值

@app.get("/items/") async def read_items(q: Union[str, None] = Query(default=None, max_length=50)): # Query适用于查询参数中,设置默认值为None,并且规定最大长度为50 results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]} if q: results.update({"q": q}) return results

不设置默认值,必填查询字符串

@app.get("/items/") async def read_items(q: str = Query(min_length=3)): results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]} if q: results.update({"q": q}) return results

不设置默认值,必填查询字符串的另一种写法(...)省略号

@app.get("/items/") async def read_items(q: str = Query(default=..., min_length=3)): results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]} if q: results.update({"q": q}) return results

不设置默认值,必填查询字符串的另一种写法Required

from pydantic import Required @app.get("/items/") async def read_items(q: str = Query(default=Required, min_length=3)): results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]} if q: results.update({"q": q}) return results

__EOF__

本文作者404 Not Found
本文链接https://www.cnblogs.com/weiweivip666/p/18041135.html
关于博主:可能又在睡觉
版权声明:转载请注明出处
声援博主:如果看到我睡觉请喊我去学习
posted @   我在路上回头看  阅读(47)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
历史上的今天:
2022-02-28 方法指针或非指针类型接收器
点击右上角即可分享
微信分享提示