Skip to content

Commit 3424394

Browse files
committed
Model can use post_init method too
1 parent 960a489 commit 3424394

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

datamodel/models.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,9 @@ class Model(ModelMixin, metaclass=ModelMeta):
6565
Basic dataclass-based Model.
6666
"""
6767
Meta = Meta
68+
69+
def __post_init__(self) -> None:
70+
"""
71+
Post init method.
72+
Useful for making Post-validations of Model.
73+
"""

datamodel/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
__title__ = 'python-datamodel'
44
__description__ = ('simple library based on python +3.8 to use Dataclass-syntax'
55
'for interacting with Data')
6-
__version__ = '0.3.10'
6+
__version__ = '0.3.11'
77
__author__ = 'Jesus Lara'
88
__author_email__ = '[email protected]'
99
__license__ = 'BSD'

examples/test_factory.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from dataclasses import is_dataclass
2+
from datetime import datetime, date
3+
from datamodel import Model, Field
4+
5+
6+
def now():
7+
return datetime.now()
8+
9+
class Environment(Model):
10+
time: datetime = Field(factory=now)
11+
dow: int
12+
hour: int
13+
date: date
14+
15+
def __post_init__(self):
16+
self.hour = self.time.hour
17+
self.dow = self.time.weekday()
18+
self.date = self.time.date
19+
super(Environment, self).__post_init__()
20+
21+
22+
env = Environment()
23+
print(env)
24+
print(is_dataclass(env))

0 commit comments

Comments
 (0)