当前位置: 首页 > news >正文

游戏工作室加盟平台辽宁好的百度seo公司

游戏工作室加盟平台,辽宁好的百度seo公司,口碑营销平台,长沙专业seo优化推荐深入解析 FastAPI 查询参数#xff1a;配置、类型转换与灵活组合 本文全面解析了 FastAPI 查询参数的使用方法#xff0c;包括配置默认值、设为可选或必选参数、类型转换以及组合使用等实用技巧。通过查询参数#xff0c;开发者可以在路径操作函数中接收动态输入#xff0…深入解析 FastAPI 查询参数配置、类型转换与灵活组合 本文全面解析了 FastAPI 查询参数的使用方法包括配置默认值、设为可选或必选参数、类型转换以及组合使用等实用技巧。通过查询参数开发者可以在路径操作函数中接收动态输入灵活地构建 API 接口。文章详细说明了如何利用类型转换实现参数的自动解析和校验同时展示了多个查询参数和路径参数组合使用的示例为开发高效、灵活的 API 提供了可靠的指导。 文章目录 深入解析 FastAPI 查询参数配置、类型转换与灵活组合一 简介二 设置默认值三 可选参数四 查询参数类型转换五 多个路径和查询参数六 必选查询参数七 组合参数八 完整代码示例九 源码地址 示例使用 Python 版本为 Python 3.10.15 。 一 简介 路径操作函数会把非路径参数自动解释为查询参数。示例 from fastapi import FastAPIapp FastAPI()fake_items_db [{item_name: Foo}, {item_name: Bar}, {item_name: Baz}]app.get(/items/) async def read_item(skip: int 0, limit: int 10):return fake_items_db[skip : skip limit]代码示例在 chapter03.py 中运行如下命令 $ uvicorn chapter03:app --reload在线 SwaggerUI 文档访问以下 URL http://127.0.0.1:8000/items/?skip0limit10查询参数为 skip值为 0limit值为 10 二 设置默认值 声明方法 async def read_item(skip: int 0, limit: int 10) 时 skip0 和 limit10 设定默认值。 访问 URL http://127.0.0.1:8000/items/与访问以下地址相同 http://127.0.0.1:8000/items/?skip0limit10但如果访问 http://127.0.0.1:8000/items/?skip20查询参数的值就是 skip20在 URL 中设定的值limit10使用默认值 三 可选参数 默认值设为 None 时查询参数为可选参数。 from fastapi import FastAPIapp FastAPI()app.get(/items/{item_id}) async def read_item(item_id: str, q: str | None None):if q:return {item_id: item_id, q: q}return {item_id: item_id}本例中查询参数 q 是可选的默认值为 None。 四 查询参数类型转换 参数还可以声明为 bool 类型FastAPI 会自动转换参数类型 from fastapi import FastAPIapp FastAPI()app.get(/items02/{item_id}) async def read_item(item_id: str, q: str | None None, short: bool False):item {item_id: item_id}# 如果 q 有值就更新if q:item.update({q: q})if not short:item.update({description: This is an amazing item that has a long description})return item本例中访问 http://127.0.0.1:8000/items02/foo?short1 http://127.0.0.1:8000/items02/foo?shortTrue http://127.0.0.1:8000/items02/foo?shorttrue http://127.0.0.1:8000/items02/foo?shorton http://127.0.0.1:8000/items02/foo?shortyes函数接收的 short 布尔类型参数都会自动转换。 五 多个路径和查询参数 from fastapi import FastAPIapp FastAPI()app.get(/users/{user_id}/items/{item_id}) async def read_user_item(user_id: int, item_id: str, q: str | None None, short: bool False ):item {item_id: item_id, owner_id: user_id}if q:item.update({q: q})if not short:item.update({description: This is an amazing item that has a long description})return itemFastAPI 通过参数名进行检测声明的查询参数的顺序并不重要。 六 必选查询参数 如果要把查询参数设置为必选就不要设置默认值。在参数声明中设置默认值则该参数就不是必选参数。把参数设为可选但又不想指定参数的默认值则要把值设为 None。 from fastapi import FastAPIapp FastAPI()app.get(/items03/{item_id}) async def read_user_item(item_id: str, needy: str):item {item_id: item_id, needy: needy}return item这里的查询参数 needy 是必选参数。访问 URL http://127.0.0.1:8000/items03/foo-item?needysooooneedy响应返回 JSON {item_id: foo-item,needy: sooooneedy }七 组合参数 from fastapi import FastAPIapp FastAPI()app.get(/items04/{item_id}) async def read_user_item(item_id: str, needy: str, skip: int 0, limit: int | None None ):item {item_id: item_id, needy: needy, skip: skip, limit: limit}return item本例中有 3 个查询参数和 1 个路径参数 needy必选的 str 类型参数skip默认值为 0 的 int 类型参数limit可选的 int 类型参数item_id必选的 str 类型参数 八 完整代码示例 from fastapi import FastAPIapp FastAPI()fake_items_db [{item_name: Foo}, {item_name: Bar}, {item_name: Baz}]app.get(/items/) async def read_item(skip: int 0, limit: int 10):return fake_items_db[skip: skip limit]app.get(/items/{item_id}) async def read_item(item_id: str, q: str | None None):if q:return {item_id: item_id, q: q}return {item_id: item_id}app.get(/items02/{item_id}) async def read_item(item_id: str, q: str | None None, short: bool False):item {item_id: item_id}# 如果 q 有值就更新if q:item.update({q: q})if not short:item.update({description: This is an amazing item that has a long description})return itemapp.get(/users/{user_id}/items/{item_id}) async def read_user_item(user_id: int, item_id: str, q: str | None None, short: bool False ):item {item_id: item_id, owner_id: user_id}if q:item.update({q: q})if not short:item.update({description: This is an amazing item that has a long description})return itemapp.get(/items03/{item_id}) async def read_user_item(item_id: str, needy: str):item {item_id: item_id, needy: needy}return itemapp.get(/items04/{item_id}) async def read_user_item(item_id: str, needy: str, skip: int 0, limit: int | None None ):item {item_id: item_id, needy: needy, skip: skip, limit: limit}return item 九 源码地址 详情见GitHub FastApiProj 引用 FastAPI 文档
http://www.hkea.cn/news/14560405/

相关文章:

  • 网站规划与开发技术属于什么大类2024装修图片100张
  • 邯郸网站建设服务平台基本型电商网站举例
  • 网站策划案怎么做福田瑞沃售后服务电话
  • 视频做网站cms app
  • 萍乡网站建设行吗隐私页 wordpress
  • 网站建设的技术团队网站建设宣传册
  • 织梦网站制作教程微信官网入口手机版
  • 做品牌网站的徐州seo推广
  • 网站开发主要框架 后端天津自动seo
  • cdr做的网站效果怎么直接用wordpress+重复插件
  • 渝北集团网站建设手机网站成功案例
  • 正能量网站不用下载直接进入wordpress 协会主题
  • 网站做两个月百度没有录取济南网站制作工具
  • 网站建设预算申请图片滤镜网站开发
  • 北滘网站建设公司菏泽网站建设优惠臻动传媒
  • 网站建设分销协议简述网站建设及维护全过程
  • 网站开发与建设方向公司部门分类
  • 帝国网站模板建设西安关键词优化排名
  • 单位网站建设框架网络推广方案推荐
  • 一个wordpress两个站点网站托管是什么
  • 网站单页推广怎么根据别人的网站做自己的网站
  • 太原网站域名开发xuzhou公司网站制作
  • 外贸网站开发莆田软件开发合同模板
  • 个人网站源码phpwordpress做书籍目录
  • 一键建网站做影视网站该怎么发展
  • 网站开发过程及要点阿里云虚拟主机建站教程
  • 大同建设局网站毕业设计网页
  • 有哪些学做衣服的网站有哪些嘉兴seo网站排名
  • php做的静态网站怎么加密网站开发实践教程
  • 国外做鞋子的网站网页制作作品免费下载