一. 响应模型
①class Item(BaseModel):
name: str
description: str = "aaaa"
price: float
tax: float = None
tags: List[str] = []
@app.post("/items/", response_model=Item)
async def create_item(item: Item):
return {"name": "aa",
"description": "aaaa",
"price": 11,
"tax": 11
}
实际返回:
{
"name": "aa",
"price": 11,
"description": "aaaa",
"tax": 11,
"tags": []
}
②
@app.post("/items/", response_model=Item)
async def create_item(item: Item):
return {"name": "aa",
"description": "aaaa",
"price": 11,
"tax": 11,
"tags":12
}
报500错误,因为"tags"键对应的值为列表
③
@app.post("/items/", response_model=Item)
async def create_item(item: Item):
return {"name": "aa",
"description": "aaaa",
"price": "aa",
"tax": 11,
"tags":[]
}
报500错误,因为"price"键对应的值为数值
@app.post("/items/", response_model=dict)
async def create_item(item: Item):
return {}
response_model=dict,那么只要返回值是字典数据都是不报错的