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

dconathan picture dconathan  路  3Comments

iwoloschin picture iwoloschin  路  3Comments

krzysieqq picture krzysieqq  路  3Comments

sommd picture sommd  路  3Comments

dmontagu picture dmontagu  路  3Comments