Skip to content

Instantly share code, notes, and snippets.

@j2labs
Created March 6, 2013 17:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save j2labs/5101136 to your computer and use it in GitHub Desktop.
Save j2labs/5101136 to your computer and use it in GitHub Desktop.
>>> from schematics.models import Model
>>> from schematics.types import StringType, IntType, DateTimeType, BooleanType
>>> from schematics.types.compound import ModelType, ListType
>>> from schematics.exceptions import ValidationError
>>> from schematics.serialize import whitelist
>>> class Game(Model):
... opponent_id = IntType(required=True)
...
>>> class Player(Model):
... total_games = IntType(min_value=0, required=True)
... name = StringType(required=True)
... verified = BooleanType()
... bio = StringType()
... games = ListType(ModelType(Game), required=True)
... class Options:
... roles = {
... 'public': whitelist('total_games', 'name', 'bio', 'games'),
... }
...
>>> p1 = Player(total_games=2,
... name='Jms Dnns',
... verified=True,
... bio='Musician | Hacker',
... games=[Game(opponent_id=1), Game(oppenent_id=2)])
>>>
>>> p2 = Player(total_games=1,
... name='Jokull Solberg',
... verified=True,
... bio='Creator of @calepinapp @oathapp and @wodboard',
... games=[Game(opponent_id=1), Game(oppenent_id=2)])
>>>
>>> p1.validate(p1.initial)
False
>>>
>>> p2.errors
{'games': {1: [u'Please use a mapping for this field'], 2: [u'Please use a mapping for this field']}}
>>>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment