2021年1月19日星期二

How to serialize nested property without showing the whole object?

I have the following object:

{    "name": "foo",    "parent": {"id": "bar", "age": 27}  }  

I would like to have this output:

{"name": "foo", "parent_id": "bar"}  

This is my Pydantic model

Model:

class TeacherOutput(PropertyBaseModel):      name: Optional[str]      parent_id: Optional[str]  

Controller:

@router.get('/teachers', response_model=List[TeacherOutput])  async def get_all():      teachers = app.model.Teacher.all()      return teachers  

I have no idea how to map just the desired property from the nested object. I don't need the other properties.

https://stackoverflow.com/questions/65790964/how-to-serialize-nested-property-without-showing-the-whole-object January 19, 2021 at 07:51PM

没有评论:

发表评论