pydantic nested model response

Question:

I want to pass nested models into the response. I am trying by making object of 1 model then storing the related values to it, making object of 2nd model and storing related values to it and at the last trying to store thoes 2 model in the response model. Can you guys please help me out. Thank you

Models

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

But I am getting error while executing this api

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

||

Answers:

You just have to create instances of the corresponding classes and pass those to your reponse model object in the constructor. In code, it should read like

response = outputResponse(
    response1= ValidateFaceAlignmentInput(currentFrame ='1'), 
    response2=SetImageSetInput(profileId ='pic1'), 
)
return response
Answered By: Simon Hawe
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.