mirror of
https://github.com/kennethreitz/pydantic.git
synced 2026-06-05 23:00:18 +00:00
91f962e533
* replace values(), with dict(), fix #98 * add history and test
36 lines
801 B
Python
36 lines
801 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())
|
|
# > {'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>
|