mirror of
https://github.com/kennethreitz/pydantic.git
synced 2026-06-05 23:00:18 +00:00
74768c1535
* Add advanced exclude support for dict, json and copy * Add advanced exclude support for dict, json and copy Add new version section (v0.31) * Add advanced include support, add more tests, improve code style Rename ValueExclude to ValueItems and move it to utils Use old logic to calculate keys, but still exclude it in _iter * Add more tests for ValueItems * Removed update arg check in _calculate_keys for return None This will increase speed when no include or exclude given and skip_defaults is False * Fix formatting, remove duplicate imports * Add # pragma: no cover to 'if TYPE_CHECKING:' block * tweaks and coverage * fix history * Add docs * tweak docs
33 lines
704 B
Python
33 lines
704 B
Python
from pydantic import BaseModel, SecretStr
|
|
|
|
class User(BaseModel):
|
|
id: int
|
|
username: str
|
|
password: SecretStr
|
|
|
|
class Transaction(BaseModel):
|
|
id: str
|
|
user: User
|
|
value: int
|
|
|
|
transaction = Transaction(
|
|
id="1234567890",
|
|
user=User(
|
|
id=42,
|
|
username="JohnDoe",
|
|
password="hashedpassword"
|
|
),
|
|
value=9876543210
|
|
)
|
|
|
|
# using a set:
|
|
print(transaction.dict(exclude={'user', 'value'}))
|
|
#> {'id': '1234567890'}
|
|
|
|
# using a dict:
|
|
print(transaction.dict(exclude={'user': {'username', 'password'}, 'value': ...}))
|
|
#> {'id': '1234567890', 'user': {'id': 42}}
|
|
|
|
print(transaction.dict(include={'id': ..., 'user': {'id'}}))
|
|
#> {'id': '1234567890', 'user': {'id': 42}}
|