1.定义一个响应体数据格式
from pydantic import BaseModel, EmailStr, Field class UserOut(BaseModel): username: str phone: str = '10086' email: EmailStr full_name: Optionnal[str] = None address: Optionnal[str] = None class UserInfo(BaseModel): username: str password: str = str phone: str = '1325678' email: EmailStr full_name: Optional[str] = None address: Optionnal[str] = None
@app04.post('/response_model', response_model=UserOut, response_model_exclude_unset=True)
async def response_model(user: UserInfo):
print(user.password)
return users['user02']
attribute 其他属性
@app04.post('/response/attribute', response_model=Union[UserInfo, UserOut])
# resonese_model = List[UserOut]
response_model_include = ['username', 'email', 'phone'] 包含的字段
response_model_exclude = ['phone'] 排除phone字段
async def resonse_attribute(user: UserInfo):
""" 当一个排除phone字段,一个包括phone字段 都包括phone字段,后边的会覆盖前边的 :param user: :return: """
del user.password
return user