Files
pydantic/docs/examples/copy_dict.py
T
Stefan Gangefors c32ce342c9 Add support for deep copying of models (#261)
Using `model.copy(deep=True)` will deep copy a module instance.

Making a deep copy is useful for models that have lists of dicts or
any other non-simple data structure to not accidentally modify data
between instances.

`model.copy()` behaviour is unchanged and will do a shallow copy of
the model instance.

fix #249
2018-09-10 11:35:58 -04:00

42 lines
1.1 KiB
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}
print(m.copy())
# > FooBarModel banana=3.14 foo='hello' bar=<BarModel whatever=123>
print(m.copy(include={'foo', 'bar'}))
# > FooBarModel foo='hello' bar=<BarModel whatever=123>
print(m.copy(exclude={'foo', 'bar'}))
# > FooBarModel banana=3.14
print(m.copy(update={'banana': 0}))
# > FooBarModel banana=0 foo='hello' bar=<BarModel whatever=123>
print(id(m.bar), id(m.copy().bar))
# normal copy gives the same object reference for `bar`
# > 140494497582280 140494497582280
print(id(m.bar), id(m.copy(deep=True).bar))
# deep copy gives a new object reference for `bar`
# > 140494497582280 140494497582856