FastAPI(14)- 路径操作函数参数的类型是一个嵌套 Pydantic Model 的使用场景

简介: FastAPI(14)- 路径操作函数参数的类型是一个嵌套 Pydantic Model 的使用场景

带有类型参数的字段


Python 有一种特定的方法来声明具有内部类型或类型参数的列表

其实前面都见过,就是

List[str]

Set[str]

Tuple[str]

Dict[str, int]

 

在 Pydantic Model 中使用 typing 提供的类型


from typing import List, Optional, Set, Dict, Tuple
from pydantic import BaseModel
class Item(BaseModel):
    name: str
    description: Optional[str] = None
    price: float
    tax: Optional[float] = None
    tags: List[str] = []
    address: Set[str] = set()
    phone: Tuple[int] = ()
    ext: Dict[str, str] = {}
item = Item(name="小菠萝", price=12.2)
print(item.dict())


输出结果

{
    "name": "小菠萝",
    "description": None,
    "price": 12.2,
    "tax": None,
    "tags": [],
    "address": set(),
    "phone": (),
    "ext": {}
}


Pydantic 嵌套模型


from typing import List
from pydantic import BaseModel
# 模型 1
class Foo(BaseModel):
    count: int
    size: float = None
# 模型 2
class Bar(BaseModel):
    apple = 'x'
    banana = 'y'
# 模型 3
class Spam(BaseModel):    
   # 字段类型是 Pydantic Model,这就是嵌套模型
    foo: Foo
    bars: List[Bar]
f = Foo(count=2)
b = Bar()
s = Spam(foo=f, bars=[b])
print(s.dict())



输出结果

{
    "bars": [
        {
            "apple": "x",
            "banana": "y"
        }
    ],
    "foo": {
        "count": 2,
        "size": 1
    }
}


FastAPI 中使用 Pydantic 嵌套模型


#!usr/bin/env python
# -*- coding:utf-8 _*-
"""
# author: 小菠萝测试笔记
# blog:  https://www.cnblogs.com/poloyy/
# time: 2021/9/19 9:40 下午
# file: 12_model.py
"""
from typing import List, Optional, Set, Dict, Tuple
import uvicorn
from pydantic import BaseModel
from fastapi import FastAPI
app = FastAPI()
class Item(BaseModel):
    name: str
    description: Optional[str] = None
    price: float
    tax: Optional[float] = None
    tags: List[str] = []
    address: Set[str] = set()
    phone: Tuple[int] = ()
    ext: Dict[str, str] = {}
# 模型一
class Image(BaseModel):
    url: str
    name: str
# 模型二
class Item(BaseModel):
    name: str
    price: float
    description: Optional[str] = None
    tags: Set[str] = set()
    # Image 模型组成的列表类型
    image: Optional[List[Image]] = None
@app.post("/items/{item_id}")
async def update_item(
        item_id: int,
        # 声明类型为:嵌套模型的 Item
        item: Item):
    results = {"item_id": item_id, "item": item}
    return results
if __name__ == "__main__":
    uvicorn.run(app="12_model:app", host="127.0.0.1", port=8080, reload=True, debug=True)


期望得到的请求体

{
  "name": "string",
  "price": 0,
  "description": "string",
  "tags": [],
  "image": [
    {
      "url": "string",
      "name": "string"
    }
  ]
}


重点

tags 虽然声明为 Set(),但在接口层面并没有集合这个概念,所以还是传数组 [ ] 格式哦,并不是传 { }

但是!集合的特性仍然会保留:去重

 

FastAPI 给嵌套模型提供的功能

和前面讲的没什么区别

  • IDE 智能代码提示,甚至对于嵌套模型也支持
  • 数据转换
  • 数据验证
  • OpenAPI 文档

 

正确传参的请求结果

image.png


校验失败的请求结果


微信图片_20220515142529.png


查看 Swagger API 文档

微信图片_20220515142547.png


深层次嵌套模型


# 更深层嵌套
from typing import List, Optional, Set
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
# 模型一
class Image(BaseModel):
    url: str
    name: str
# 模型二
class Item(BaseModel):
    name: str
    price: float
    tags: Optional[Set[str]] = set()
    images: Optional[List[Image]] = None
# 模型三
class Offer(BaseModel):
    name: str
    description: Optional[str] = None
    items: List[Item]
@app.post("/offers/")
async def create_offer(offer: Offer):
    return offer


期望得到的请求体

{
    "name": "string",
    "description": "string",
    "items": [
        {
            "name": "string",
            "price": 0,"tags": [],
            "images": [
                {
                    "url": "string",
                    "name": "string"
                }
            ]
        }
    ]
}


正确传参的请求结果

image.png


IDE 提供的智能提示

即使是三层嵌套模型,也可以拥有丝滑般的代码提示哦

image.png

相关文章
|
9月前
|
数据采集 JSON JavaScript
全面拥抱FastApi —三大参数及验证
全面拥抱FastApi —三大参数及验证
|
SQL Oracle 关系型数据库
FastAPI数据库系列(一) MySQL数据库操作 一、简介
FastAPI中你可以使用任何关系型数据库,可以通过SQLAlchemy将其轻松的适应于任何的数据库,比如: PostgreSQL MySQL SQLite Oracle Microsoft SQL Server ...
|
4天前
|
Python
Fastapi进阶用法,路径参数,路由分发,查询参数等详解
Fastapi进阶用法,路径参数,路由分发,查询参数等详解
|
4天前
|
Java API 数据库
FastAPI中如何调用同步函数
FastAPI中如何调用同步函数
53 0
|
12月前
|
网络安全 Windows
基于fastapi实现6个接口(token拦截, 2个业务流程,接口参数依赖校验)已经通过postman测试,记录部署服务器和windows,用于pytest接口自动化框架的接口测试对象
基于fastapi实现6个接口(token拦截, 2个业务流程,接口参数依赖校验)已经通过postman测试,记录部署服务器和windows,用于pytest接口自动化框架的接口测试对象
|
前端开发 中间件 API
FastAPI第二天---参数校验
FastAPI第二天---参数校验
184 0
FastAPI第二天---参数校验
|
Python
FastApi-07-查询参数校验
FastApi-07-查询参数校验
204 0
|
JSON 前端开发 数据格式
fastapi(65)- 路由函数指定了 response_model,在返回自定义 JSONResponse 时, 不会限制它返回的数据结构
fastapi(65)- 路由函数指定了 response_model,在返回自定义 JSONResponse 时, 不会限制它返回的数据结构
275 0
fastapi(65)- 路由函数指定了 response_model,在返回自定义 JSONResponse 时, 不会限制它返回的数据结构