mirror of
https://github.com/kennethreitz/pydantic.git
synced 2026-06-05 23:00:18 +00:00
f55515820a
* renaming .json -> .model_dump_json * renaming .dict -> .model_dump * renaming .__fields__ -> .model_fields * renaming .schema -> .model_json_schema * renaming .construct -> .model_construct * renaming .parse_obj -> .model_validate * make linters happy * add changes md-file Co-authored-by: Samuel Colvin <s@muelcolvin.com>
30 lines
716 B
Python
30 lines
716 B
Python
from pydantic import BaseModel
|
|
|
|
|
|
class User(BaseModel):
|
|
id: int
|
|
age: int
|
|
name: str = 'John Doe'
|
|
|
|
|
|
original_user = User(id=123, age=32)
|
|
|
|
user_data = original_user.model_dump()
|
|
print(user_data)
|
|
fields_set = original_user.__fields_set__
|
|
print(fields_set)
|
|
|
|
# ...
|
|
# pass user_data and fields_set to RPC or save to the database etc.
|
|
# ...
|
|
|
|
# you can then create a new instance of User without
|
|
# re-running validation which would be unnecessary at this point:
|
|
new_user = User.model_construct(_fields_set=fields_set, **user_data)
|
|
print(repr(new_user))
|
|
print(new_user.__fields_set__)
|
|
|
|
# construct can be dangerous, only use it with validated data!:
|
|
bad_user = User.model_construct(id='dog')
|
|
print(repr(bad_user))
|