{
"nome": "Jess",
"idade": 42,
"linguagens": [
"Python",
"Go",
"R"
]
}
pessoa/
├── __init__.py
└── models.py
from schematics.models import Model
from schematics.types import StringType, ListType, IntType
class Pessoa(Model):
nome = StringType(required=True)
idade = IntType()
linguagens = ListType(StringType)
>>> from pessoa.models import Pessoa
>>> jess_data = {
"nome": "Jess",
"idade": 42,
"linguagens": [
"Python",
"Go",
"R"
]
}
>>> jess = Pessoa(jess_data)
>>> jess.validate()
>>> from pessoa.models import Pessoa
>>> jess_data = {
"nome": "Jess",
}
>>> jess = Pessoa(jess_data)
>>> jess.to_native()
{
"nome": "Jess",
"idade": None,
"linguagens": None
}
from schematics.models import Model
from schematics.types import StringType, ListType, IntType
class Pessoa(Model):
nome = StringType(required=True)
idade = IntType(serialize_when_none=False)
linguagens = ListType(StringType, serialize_when_none=False)
>>> from pessoa.models import Pessoa
>>> jess_data = {
"nome": "Jess",
}
>>> jess = Pessoa(jess_data)
>>> jess.to_native()
{
"nome": "Jess"
}
from schematics.models import Model
from schematics.types import StringType, ListType, ModelType, IntType
class Pessoa(Model):
nome = StringType(required=True, serialize_when_none=False,
deserialize_from="name")
idade = IntType(serialize_when_none=False, deserialize_from="age")
linguagens = ListType(StringType, serialize_when_none=False,
deserialize_from="lang")
>>> from pessoa.models import Pessoa
>>> jess_data = {
"name": "Jess",
"age": "42",
"lang": [
"Python",
"Go",
"R"
]
}
>>> jess = Pessoa(jess_data)
>>> jess.validate()
>>> jess.to_native()
{
'nome': 'Jess',
'idade': 42,
'linguagens': [
'Python',
'Go',
'R'
]
}
from schematics.models import Model
from schematics.types import StringType, ListType, ModelType, IntType
class Pessoa(Model):
nome = StringType(required=True, serialize_when_none=False,
deserialize_from="name", serialized_name="name")
idade = IntType(serialize_when_none=False, deserialize_from="age",
serialized_name="age")
linguagens = ListType(StringType, serialize_when_none=False,
deserialize_from="lang", serialized_name="blabla")
>>> from pessoa.models import Pessoa
>>> jess_data = {
"name": "Jess",
"age": "42",
"lang": [
"Python",
"Go",
"R"
]
}
>>> jess = Pessoa(jess_data)
>>> jess.validate()
>>> jess.to_native()
{
'name': 'Jess',
'age': 42,
'blabla': [
'Python',
'Go',
'R'
]
}
{
"name": "Jess",
"age": "42",
"lang": [
"Python",
"Go",
"R"
],
"pets": [{
"name": "Channel",
"fur": "branca"
},
{
"name": "Cora",
"fur": "preta"
},
{
"name": "Catarina",
"fur": "loira"
}
]
}
from schematics.models import Model
from schematics.types import StringType, ListType, ModelType, IntType
class PetType(Model):
nome = StringType(required=True, deserialize_from="name",
serialized_name="name")
cor = StringType(required=True, deserialize_from="fur",
serialized_name="fur")
class Pessoa(Model):
nome = StringType(required=True, deserialize_from="name",
serialized_name="name")
idade = IntType(serialize_when_none=False, deserialize_from="age",
serialized_name="age")
linguagens = ListType(StringType, serialize_when_none=False,
deserialize_from="lang", serialized_name="blabla")
pets = ListType(ModelType(PetType))
>>> from pessoa.models import Pessoa
>>> jess_data = {
"name": "Jess",
"age": "42",
"lang": [
"Python",
"Go",
"R"
],
"pets": [
{"name": "cora", "fur": "preta"},
{"name": "channel", "fur": "branca"}
]
}
>>> jess = Pessoa(jess_data)
>>> jess.validate()