-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuser_model.py
38 lines (29 loc) · 1.15 KB
/
user_model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from typing import List
from pydantic import BaseModel
# Define a Pydantic model
class User(BaseModel):
id: int
name: str
age: int
# Create an instance of the model
user = User(id=1, name="John Doe", age=30)
# 1. Print the field names (keys)
print("Model Fields (Keys):", user.model_fields.keys())
# 2. Print detailed information about the fields
print("\nDetailed Field Info:")
for field_name, field_info in user.model_fields.items():
print(f"{field_name}: {field_info}")
# 3. Print the model's configuration (useful metadata like validation settings)
print("\nModel Config:")
print(user.model_config)
# 4. Access the model as a dictionary (all keys and values)
print("\nModel as a dictionary:")
print(user.model_dump())
# Create individual instances of the model
# Create a list of Pydantic objects
users: List[User] = [user1, user2, user3]
print(users[0].name)