FastAPI学习笔记(一)-10.嵌套请求体和返回结果
1 ''' 2 @author:invoker 3 @project:fastapi202108 4 @file: chapter035.py 5 @contact:invoker2021@126.com 6 @descript: 7 @Date:2021/8/6 14:31 8 @version: Python 3.7.8 9 ''' 10 from fastapi import APIRouter 11 from pydantic import BaseModel, Field 12 from typing import List 13 from datetime import date 14 15 app035 = APIRouter() 16 17 """ 18 3.5 数据格式嵌套的请求体 19 """ 20 21 22 class CityInfo(BaseModel): 23 name: str = Field(..., min_length=5, title='城市名称') 24 country: str = Field(..., min_lenght=5, title='国家名称') 25 country_code: str = Field(default='', title='国家编码') 26 country_population: int = Field(dafault=800, ge=800, title='人口数量') 27 28 class Config: 29 schema_extra = { 30 "example": { 31 "name": "Wuhan", 32 "country": "China", 33 "country_code": "CN", 34 "country_population": 10000000 35 } 36 } 37 38 39 class Data(BaseModel): 40 city: List[CityInfo] = None # 定义数据格式嵌套的请求体 41 date: date # 额外数据类型 https://fastapi.tiangolo.com/tutorial/extra-data-types/ 42 confirmed: int = Field(default=0, title='确诊人数', ge=0) 43 death: int = Field(default=0, title='死亡人数', ge=0) 44 recovered: int = Field(default=0, title='痊愈数', ge=0) 45 46 @app035.put('/request_body/nested') 47 async def nested_data(data:Data): 48 return data
请求体:
返回值也是嵌套展示:
本文来自博客园,作者:kaer_invoker,转载请注明原文链接:https://www.cnblogs.com/invoker2021/p/15108701.html