1
1
from typing import List , Optional
2
2
from datamodel import BaseModel , Field
3
- from datamodel .aliases import to_snakecase
3
+ from datamodel .aliases import to_snakecase , to_pascalcase
4
4
5
5
6
6
class Store (BaseModel ):
@@ -13,6 +13,37 @@ class Meta:
13
13
as_objects = True
14
14
alias_function = to_snakecase
15
15
16
- # Example Usage:
17
- store = Store (
EmailAddress = "[email protected] " ,
Status = "ACTIVE" ,
StoreId = 1 )
18
- print (store )
16
+
17
+
18
+ class ExampleModel (BaseModel ):
19
+ # Here, our Python fields are declared in PascalCase.
20
+ StoreId : int = Field (primary_key = True )
21
+ EmailAddress : str
22
+ Status : str
23
+
24
+ class Meta :
25
+ strict = True
26
+ as_objects = True
27
+ # Our alias_function now transforms from the incoming snake_case to PascalCase.
28
+ # So if user passes `store_id`, we transform it to `StoreId`.
29
+ alias_function = to_pascalcase
30
+
31
+ def demo ():
32
+ store = Store (
EmailAddress = "[email protected] " ,
Status = "ACTIVE" ,
StoreId = 1 )
33
+ print ("StoreId =>" , store .store_id )
34
+ print ("EmailAddress =>" , store .email_address )
35
+ # The user input is using snake_case:
36
+ model = ExampleModel (
37
+ store_id = 123 ,
38
+ email_address = "[email protected] " ,
39
+ status = "ACTIVE"
40
+ )
41
+ # Internally, we have PascalCase attributes:
42
+ print ("StoreId =>" , model .StoreId )
43
+ print ("EmailAddress =>" , model .EmailAddress )
44
+ print ("Status =>" , model .Status )
45
+ # Check the actual model
46
+ print (model )
47
+
48
+ if __name__ == "__main__" :
49
+ demo ()
0 commit comments