一. 響應(yīng)模型
①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
}
實(shí)際返回:
{
"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
}
報(bào)500錯(cuò)誤忽肛,因?yàn)?tags"鍵對(duì)應(yīng)的值為列表
③
@app.post("/items/", response_model=Item)
async def create_item(item: Item):
return {"name": "aa",
"description": "aaaa",
"price": "aa",
"tax": 11,
"tags":[]
}
報(bào)500錯(cuò)誤,因?yàn)?price"鍵對(duì)應(yīng)的值為數(shù)值
@app.post("/items/", response_model=dict)
async def create_item(item: Item):
return {}
response_model=dict烂斋,那么只要返回值是字典數(shù)據(jù)都是不報(bào)錯(cuò)的