Skip to content
Advertisement

override field alias from subclass in Pydantic

I have 2 models, 1 subclassing the other:

JavaScript

How can I accomplish this ?

Advertisement

Answer

You don’t need to subclass to accomplish what you want (unless your need is more complex than your example).

For import: Add the Config option to allow_population_by_field_name so you can add the data with names or firstnames

For export: Add by_alias=True to the dict() method to control the output

JavaScript

yields:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement