【问题标题】:pydantic nested model responsepydantic 嵌套模型响应
【发布时间】:2022-07-28 01:47:40
【问题描述】:

我想将嵌套模型传递到响应中。我试图通过制作 1 个模型的对象然后将相关值存储到它,制作第 2 个模型的对象并将相关值存储到它,最后尝试将 2 个模型存储在响应模型中。你们能帮帮我吗。谢谢

模型

class SetImageSetInput(BaseModel):
  profileId: str
  
  class Config:
    orm_mode = True



class ValidateFaceAlignmentInput(BaseModel):
  currentFrame: str
  
  class Config:
    orm_mode = True

class outputResponse(BaseModel):
    response1: ValidateFaceAlignmentInput
    response2: SetImageSetInput


SetImageSetInput.profileId="pic1"
ValidateFaceAlignmentInput.currentFrame="1"
outputResponse.response1 = SetImageSetInput
outputResponse.response2 = ValidateFaceAlignmentInput

return SetImageSetInput

但是我在执行这个 api 时遇到了错误

pydantic.error_wrappers.ValidationError: 1 validation error for outputResponse
response -> response1
  field required (type=value_error.missing)

【问题讨论】:

    标签: python fastapi pydantic


    【解决方案1】:

    您只需创建相应类的实例并将它们传递给构造函数中的响应模型对象。在代码中,它应该是这样的

    response = outputResponse(
        response1= ValidateFaceAlignmentInput(currentFrame ='1'), 
        response2=SetImageSetInput(profileId ='pic1'), 
    )
    return response
    

    【讨论】:

    • 抱歉,打错了。你能不能再试一次。它在这里工作。
    猜你喜欢
    • 2022-01-15
    • 2021-09-06
    • 2017-09-05
    • 2022-11-26
    • 2021-11-13
    • 1970-01-01
    • 2021-03-04
    • 2022-06-24
    • 2023-02-25
    相关资源
    最近更新 更多