响应模型
返回与输入相同的模型
永远不要存储用户的明文密码,也不要在响应中发送密码。
from typing import Union
from fastapi import FastAPI
from pydantic import BaseModel, EmailStr
app = FastAPI()
class UserIn(BaseModel):
username: str
password: str
email: EmailStr
full_name: Union[str, None] = None
@app.post("/user/")
async def create_user(user: UserIn) -> UserIn:
return user
Post请求
http://127.0.0.1:8000/user/
{
"username": "alex",
"password": "12345",
"email": "[email protected]"
}
返回信息
{
"username": "alex",
"password": "12345",
"email": "[email protected]",
"full_name": null
}
返回与输入不同的模型
创建一个有明文密码的输入模型和一个没有明文密码的输出模型
你可以在任意的路径操作中使用
response_model
参数来声明用于响应的模型:
@app.get()
@app.post()
@app.put()
@app.delete()
from typing import Any, Union
from fastapi import FastAPI
from pydantic import BaseModel, EmailStr
app = FastAPI()
# 输入
class UserIn(BaseModel):
username: str
password: str
email: EmailStr
full_name: Union[str, None] = None
# 输出
class UserOut(BaseModel):
username: str
email: EmailStr
full_name: Union[str, None] = None
@app.post("/user/", response_model=UserOut)
async def create_user(user: UserIn) -> Any:
return user
Post请求
http://127.0.0.1:8000/user/
{
"username": "alex",
"password": "12345",
"email": "[email protected]"
}
返回信息
{
"username": "alex",
"email": "[email protected]",
"full_name": null
}
过滤字段的默认值
可以设置路径操作装饰器中的
response_model_exclude_unset=True
参数,然后响应中将不会包含那些默认值,而是仅有实际设置的值
同一个模型
from typing import Any, Union
from fastapi import FastAPI
from pydantic import BaseModel, EmailStr
app = FastAPI()
class UserIn(BaseModel):
username: str
password: str
email: Union[EmailStr, None] = None
full_name: Union[str, None] = None
@app.post("/user/", response_model=UserIn, response_model_exclude_unset=True)
async def create_user(user: UserIn) -> Any:
return user
Post请求
http://127.0.0.1:8000/user/
{
"username": "alex",
"password": "12345"
}
返回信息
{
"username": "alex",
"password": "12345"
}
不同模型
使用
response_model_exclude_unset=True
会失效
from typing import Any, Union
from fastapi import FastAPI
from pydantic import BaseModel, EmailStr
app = FastAPI()
class UserIn(BaseModel):
username: str
password: str
email: Union[EmailStr, None] = None
full_name: Union[str, None] = None
class UserOut(BaseModel):
username: str
email: Union[EmailStr, None] = None
full_name: Union[str, None] = None
@app.post("/user/", response_model=UserOut, response_model_exclude_unset=True)
async def create_user(user: UserIn) -> Any:
return user
Post请求
http://127.0.0.1:8000/user/
{
"username": "alex",
"password": "12345"
}
返回信息
{
"username": "alex",
"email": null,
"full_name": null
}
解决不同模型不过滤默认值
使用
response_model_exclude_defaults=True
或response_model_exclude_none=True
也是可以达到一样的效果的
from typing import Any, Union
from fastapi import FastAPI
from pydantic import BaseModel, EmailStr
app = FastAPI()
class UserIn(BaseModel):
username: str
password: str
email: Union[EmailStr, None] = None
full_name: Union[str, None] = None
class UserOut(BaseModel):
username: str
email: Union[EmailStr, None] = None
full_name: Union[str, None] = None
@app.post("/user/", response_model=UserOut, response_model_exclude_none=True)
async def create_user(user: UserIn) -> Any:
return user
Post请求
http://127.0.0.1:8000/user/
{
"username": "alex",
"password": "12345"
}
返回信息
{
"username": "alex"
}
设置返回的字段
这是因为即使使用
response_model_include
或response_model_exclude
来省略某些属性,在应用程序的 OpenAPI 定义(和文档)中生成的 JSON Schema 仍将是完整的模型,这也适用于作用类似的response_model_by_alias
。使用
list
或tuple
,FastAPI 仍会将其转换为set
并且正常工作,如response_model_exclude=[xx,xx]
response_model_exclude不允许字段
from typing import Any, Union
from fastapi import FastAPI
from pydantic import BaseModel, EmailStr
app = FastAPI()
class UserIn(BaseModel):
username: str
password: str
email: Union[EmailStr, None] = None
full_name: Union[str, None] = None
# 过滤返回字段password
@app.post("/user/", response_model=UserIn, response_model_exclude={"password"})
async def create_user(user: UserIn) -> Any:
return user
Post请求
http://127.0.0.1:8000/user/
{
"username": "alex",
"password": "12345"
}
返回信息
{
"username": "alex",
"email": null,
"full_name": null
}
response_model_include允许字段
from typing import Any, Union
from fastapi import FastAPI
from pydantic import BaseModel, EmailStr
app = FastAPI()
class UserIn(BaseModel):
username: str
password: str
email: Union[EmailStr, None] = None
full_name: Union[str, None] = None
# 只允许返回username和email
@app.post("/user/", response_model=UserIn, response_model_include={"username", "email"})
async def create_user(user: UserIn) -> Any:
return user
Post请求
http://127.0.0.1:8000/user/
{
"username": "alex",
"password": "12345"
}
返回信息
{
"username": "alex",
"email": null
}