Is it possible to change the output alias in pydantic?

Update (2023-10-07): Check the comments in the question for other answers and this answer in the same question for pydantic 2.0 or newer.


Switch aliases and field names and use the allow_population_by_field_name model config option:

class TMDB_Category(BaseModel):
    strCategory: str = Field(alias="name")
    strCategoryDescription: str = Field(alias="description")

    class Config:
        allow_population_by_field_name = True

Let the aliases configure the names of the fields that you want to return, but enable allow_population_by_field_name to be able to parse data that uses different names for the fields.

Leave a Comment