赞
踩
Request Body and Fields请求体和字段
class CityInfo(BaseModel): city_name: str = Field(...,example='城市名称') #example是注解的作用,值不会被验证 country_name: Optional[str]=None population : int = Field(default=800,description='城市人口',ge=800) class Config: schema_extra={ 'example': { 'city_name': 'Shanghai', 'country_name': 'China', 'population': 36648000, } } @app01.post('/city/') async def city_info(city: CityInfo): # return {'name': city.city_name, # 'country_name': city.country_name, # 'population': city.population # } return city.dict()
混合参数
"""混合参数""" @app01.put('/city_alter/{city_name}') async def city_alter( city_name:str, city1:CityInfo, city2:CityInfo, all_population: int = Query(default=140000000,description='国家总人口') ): if city_name == 'Shanghai': return { 'city_name': 'Shanghai', 'country_name': 'China', 'population': 36648000, 'all_popuplation': all_population } return city1.dict(),city2.dict()
嵌套请求体
"""嵌套请求体""" class CityInfo(BaseModel): city_name: str = Field(...,example='城市名称') #example是注解的作用,值不会被验证 country_name: Optional[str]=None population : int = Field(default=800,description='城市人口',ge=800) class Config: schema_extra={ 'example': { 'city_name': 'Shanghai', 'country_name': 'China', 'population': 36648000, } } class Data(BaseModel): city_list:List[CityInfo]=None #这里就是定义数据格式嵌套的请求体 total_population:int = Field(description='人口总数',default=1200000) date:date @app01.post('/data/') def get_data(data:Data): return data.dict()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。