Files
pydantic/docs/examples/copy_values.py
T
Samuel Colvin 92d7689271 Immutability part 2 (#53)
* add construct method, fix #48
* adding copy method
* adding pickle support, fix #40
* tweak copy and add fields copy test
* adding docs for immutability, values and copy
* add docs for pickle
2017-06-21 18:15:08 +01:00

36 lines
807 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.values())
# > {'banana': 3.14, 'foo': 'hello', 'bar': {'whatever': 123}}
print(m.values(include={'foo', 'bar'}))
# > {'foo': 'hello', 'bar': {'whatever': 123}}
print(m.values(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>