问答详情
源自:4-1 响应模型示例精讲

我写的response_model_include和response_model_exclude没有效果

@app04.post(
    "/response_model/attributes",
    #response_model=UserOut,
    #response_model=Union[UserIn, UserOut]#取并集
    #response_model=List[UserOut] #也可以用列表,响应多个模型类return [user,user]
    response_model_include=["username", "email"] #当响应字段太多时指定包含或排除哪些字段
    # response_model_exclude={"mobile"}

)
async def response_model_attributes(user: UserIn):
    #del user.password #Union[UserIn, UserOut]后,删除password属性
    return user

我试了好几次,,返回都是返回了全部字段;

http://img1.sycdn.imooc.com/619cbe2e0001712e06700467.jpg

提问者:慕少1021562 2021-11-23 18:11

个回答

  • qq_慕仔3049912
    2022-11-12 22:25:16

    把[],改成{}:

    response_model_include={'username','mobile'},

    还有。model不能少,不然也不行:

    esponse_model=UserOut,


  • 宝慕林1380583
    2021-12-25 13:11:23

    response_model_exclude_unset=True