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
29 lines
466 B
Python
29 lines
466 B
Python
from pydantic import BaseModel
|
|
|
|
|
|
class FooBarModel(BaseModel):
|
|
a: str
|
|
b: dict
|
|
|
|
class Config:
|
|
allow_mutation = False
|
|
|
|
|
|
foobar = FooBarModel(a='hello', b={'apple': 'pear'})
|
|
|
|
try:
|
|
foobar.a = 'different'
|
|
except TypeError as e:
|
|
print(e)
|
|
# > "FooBarModel" is immutable and does not support item assignment
|
|
|
|
print(foobar.a)
|
|
# > hello
|
|
|
|
print(foobar.b)
|
|
# > {'apple': 'pear'}
|
|
|
|
foobar.b['apple'] = 'grape'
|
|
print(foobar.b)
|
|
# > {'apple': 'grape'}
|