Pydantic: Is there a simple way to convert from Pydantic models?

Created on 2 Apr 2020  路  2Comments  路  Source: samuelcolvin/pydantic

Hello,

I know that it is possible to convert to Pydantic models using the method from_orm().

However, I don't find anywhere information about doing the opposite way, i.e. converting Pydantic models to ORM classes (something like to_orm()).

Is there a simple way to accomplish this? For example, in my case I have a lot of nested models (models which contain lists of other models) and I want to convert to my business objects.

Thanks!

question

Most helpful comment

The general approach is to use something like MyOrmModel(**my_pydantic_model.dict()).

Any other solution would either require specific integration with an ORM or would need to be implemented in the other model.

All 2 comments

The general approach is to use something like MyOrmModel(**my_pydantic_model.dict()).

Any other solution would either require specific integration with an ORM or would need to be implemented in the other model.

Thank you for the answer.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dand-oss picture dand-oss  路  19Comments

jasonkuhrt picture jasonkuhrt  路  21Comments

koxudaxi picture koxudaxi  路  25Comments

samuelcolvin picture samuelcolvin  路  30Comments

marlonjan picture marlonjan  路  37Comments