5

I have a model:

class Cars(BaseModel):
 numberOfCars: int = Field(0,alias='Number of cars')

I have a dict with:

{
"Number of cars":3
}

How can I create an instance of Cars by using this model?` Is there something like 'by_alias' when using this?

FishingIsLife
  • 1,972
  • 3
  • 28
  • 51

1 Answers1

7

You should definitely start by reading the Pydantic Documentation

Here is a working example:

from pydantic import BaseModel, Field


class Cars(BaseModel):
    numberOfCars: int = Field(0, alias='Number of cars')


def main():
    car_dict = {'Number of cars': 4}
    cars = Cars(**car_dict)
    print(cars)


if __name__ == '__main__':
    main()