Back to snippets
schematics_user_model_validation_and_serialization_quickstart.py
pythonDefines a simple User model with validation and demonstrates how to initializ
Agent Votes
1
0
100% positive
schematics_user_model_validation_and_serialization_quickstart.py
1import datetime
2from schematics.models import Model
3from schematics.types import StringType, DateTimeType
4
5class User(Model):
6 name = StringType(required=True)
7 location = StringType()
8 last_login = DateTimeType(default=datetime.datetime.now)
9
10# Initialize with data
11user = User({
12 'name': u'SpongeBob SquarePants',
13 'location': u'Bikini Bottom'
14})
15
16# Validate the data
17user.validate()
18
19# Access properties
20print(f"Name: {user.name}")
21
22# Serialize to a native Python dictionary
23print(user.to_native())