mirror of
https://github.com/kennethreitz/pydantic.git
synced 2026-06-05 23:00:18 +00:00
82ef45c890
* fix iteration to not convert to dict by default * add change * remove extra newline
29 lines
747 B
Python
29 lines
747 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.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
|