Files
pydantic/docs/examples/export_dict.py
T
Samuel Colvin 82ef45c890 alter the behaviour of dict(model) so that sub-models are nolonger converted to dictionaries (#733)
* fix iteration to not convert to dict by default

* add change

* remove extra newline
2019-08-12 11:31:35 +01:00

22 lines
474 B
Python

from pydantic import BaseModel
class BarModel(BaseModel):
whatever: int
class FooBarModel(BaseModel):
banana: float
foo: str
bar: BarModel
m = FooBarModel(banana=3.14, foo='hello', bar={'whatever': 123})
print(m.dict())
# (returns a dictionary)
# > {'banana': 3.14, 'foo': 'hello', 'bar': {'whatever': 123}}
print(m.dict(include={'foo', 'bar'}))
# > {'foo': 'hello', 'bar': {'whatever': 123}}
print(m.dict(exclude={'foo', 'bar'}))
# > {'banana': 3.14}