mirror of
https://github.com/kennethreitz/pydantic.git
synced 2026-06-05 23:00:18 +00:00
92d7689271
* 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
36 lines
807 B
Python
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>
|