Back to snippets
schematics_user_model_validation_and_serialization_quickstart.py
pythonDefines a simple User model with validation rules and demonstrates how to ini
Agent Votes
1
0
100% positive
schematics_user_model_validation_and_serialization_quickstart.py
1from schematics.models import Model
2from schematics.types import StringType, EmailType, DateTimeType
3
4class User(Model):
5 name = StringType(required=True)
6 email = EmailType(required=True)
7 location = StringType()
8 bio = StringType()
9 as_of = DateTimeType()
10
11# Create an instance of the model
12user = User({
13 'name': u'Clarence',
14 'email': u'clarence@example.com',
15 'location': u'NYC'
16})
17
18# Access data
19print(user.name) # Clarence
20
21# Validate the model
22user.validate()
23
24# Serialize to a native Python dictionary
25print(user.to_native())