mirror of
https://github.com/kennethreitz/pydantic.git
synced 2026-06-05 23:00:18 +00:00
664cbcfc01
* Add private attributes support * Add more blank lines in example * Add changes file * Update docs/usage/models.md Co-authored-by: Samuel Colvin <samcolvin@gmail.com> * fix after bad merge * Add PrivateAttr, Config.underscore_attrs_are_private * remove unrelated change in utils.py * add # noqa: C901 (ignore complexity) to __setattr__ (see comment in PR) * add annotation to Config.underscore_attrs_are_private Co-authored-by: Samuel Colvin <samcolvin@gmail.com> * use sunder names * mention underscore_attrs_are_private in model_config.md * add comment about default factory * fix comment * fix comment * clarify that both dunder and sunder names might be used * tweak docs and name * _set_default_private_attributes -> _init_private_attributes Co-authored-by: Samuel Colvin <samcolvin@gmail.com> * use new name _init_private_attributes * move tests * copy private attributes in BaseModel.copy() * add test for default and default_factory used together * fix linting * more tests, default_factory kw only Co-authored-by: Samuel Colvin <samcolvin@gmail.com> Co-authored-by: Samuel Colvin <s@muelcolvin.com>
20 lines
481 B
Python
20 lines
481 B
Python
from datetime import datetime
|
|
from random import randint
|
|
|
|
from pydantic import BaseModel, PrivateAttr
|
|
|
|
|
|
class TimeAwareModel(BaseModel):
|
|
_processed_at: datetime = PrivateAttr(default_factory=datetime.now)
|
|
_secret_value: str = PrivateAttr()
|
|
|
|
def __init__(self, **data):
|
|
super().__init__(**data)
|
|
# this could also be done with default_factory
|
|
self._secret_value = randint(1, 5)
|
|
|
|
|
|
m = TimeAwareModel()
|
|
print(m._processed_at)
|
|
print(m._secret_value)
|