The code below is modified from the Pydantic documentation
I would like to know how to change BarModel
and FooBarModel
so they accept the input assigned to m1
. I have tried using __root__
and syntax such as Dict[str, BarModel]
but have been unable to find the magic combination.
from pydantic import BaseModel
class BarModel(BaseModel):
whatever: float
foo: str
class FooBarModel(BaseModel):
banana: str
bar: BarModel
m = FooBarModel(banana='a', bar={'whatever': 12.3, 'foo':'hello'})
m1 = FooBarModel({
'a':{'whatever': 12.3, 'foo':'hello'},
'b':{'whatever': 12.4, 'foo':'bye'}
})
print(m.dict()) # returns a dictionary:
print(m1.dict()) # TypeError
The final solution will be deployed in a FastAPI context.