FastAPI 响应模型
作者:互联网
使用response_model
参数,即可在以下路径参数中声明响应模型:
@app.get()
@app.put()
@app.post()
@app.delete()
from typing import List, Optional
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
name: str
description: Optional[str] = None
price: float
tax: Optional[float] = None
tags: List[str] = []
@app.post("/items/", response_model=Item)
async def create_item(item: Item):
return item
注意:response_model
是装饰器方法的参数,与之前的参数和请求体不同,他不是路径操作函数的参数。
response_model
接收的类型与声明Pydantic模型属性的类型相同,可以是Pydantic模型,也可以是Pydantic模型列表,例如:List[Item]
返回相同的输入数据
from typing import Optional
from fastapi import FastAPI
from pydantic import BaseModel, EmailStr
app = FastAPI()
class UserIn(BaseModel):
username: str
password: str
email: EmilStr
full_name: Optional[str] = None
@app.post("/user/", response_model=UserIn)
async def create_user(user: UserIn):
return user
使用此模型声明输入对象,并使用同一个模型声明输出对象:
from typing import Optional
from fastapi import FastAPI
from pydantic import BaseModel, EmailStr
app = FastAPI()
class UserIn(BaseModel):
username: str
password: str
email: EmailStr
full_name: Optional[str] = None
# Don't do this in production!
@app.post("/user/", response_model=UserIn)
async def create_user(user: UserIn):
return user
现在,只要在浏览器中使用密码创建用户,API就会在响应中国返回相同的密码。本例中,因为是用户本人发送密码,这种操作没什么问题,但是如果在其他路径操作中使用同一个模型,就会把用户的密码发送给每一个客户端。
添加输出模型
相对于包含明文密码的输入模型,创建不含明文密码的输出模型:
from typing import Optional
from fastapi import FastAPI
from pydantic import BaseModel, EmailStr
app = FastAPI()
class UserIn(BaseModel):
username: str
password: str
email: EmailStr
full_name: Optional[str] = None
class UserOut(BaseModel):
username: str
email: EmailStr
full_name: Optional[str] = None
@app.post("/user/", response_model=UserOut)
async def create_user(user: UserIn):
return user
这样,即便路径操作函数返回同样的输入用户:
from typing import Optional
from fastapi import FastAPI
from pydantic import BaseModel, EmailStr
app = FastAPI()
class UserIn(BaseModel):
username: str
password: str
email: EmailStr
full_name: Optional[str] = None
class UserOut(BaseModel):
username: str
email: EmailStr
full_name: Optional[str] = None
@app.post("/user/", response_model=UserOut)
async def create_user(user: UserIn):
return user
但因为response_model中声明的UserOut模型没有包含密码:
from typing import Optional
from fastapi import FastAPI
from pydantic import BaseModel, EmailStr
app = FastAPI()
class UserIn(BaseModel):
username: str
password: str
email: EmailStr
full_name: Optional[str] = None
class UserOut(BaseModel):
username: str
email: EmailStr
full_name: Optional[str] = None
@app.post("/user/", response_model=UserOut)
async def create_user(user: UserIn):
return user
FastAPI会过滤掉所有未在输出模型中声明的数据。
响应模型编码参数
响应模型支持默认值:
from typing import List, Optional
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
name: str
description: Optional[str] = None
price: float
tax: float = 10.5
tags: List[str] = []
items = {
"foo": {"name": "Foo", "price": 50.2},
"bar": {"name": "Bar", "description": "The bartenders", "price": 62, "tax": 20.2},
"baz": {"name": "Baz", "description": None, "price": 50.2, "tax": 10.5, "tags": []},
}
@app.get("/items/{item_id}", response_model=Item, response_model_exclude_unset=True)
async def read_item(item_id: str):
return items[item_id]
description: Optional[str] = None
的默认值是None
tax: float = 10.5
的默认值是10.5
tags: List[str] = []
的默认值是空列表:[]
但如果没有为含默认值的属性另赋新值,输出结果会省略含默认值的属性。
例如,NoSQL 数据库的模型中往往包含很多可选属性,如果输出含默认值的属性,输出的 JSON 响应会特别长,此时,可以省略只含默认值的属性。
标签:FastAPI,模型,响应,user,str,BaseModel,import,Optional,app 来源: https://www.cnblogs.com/litianming/p/16225540.html