from schematics.models import Model
from schematics.types import StringType, URLType
import json
class Person(Model):
name = StringType(required=True) #不能为空
website = URLType()
person = Person({'name':'sss',
'website': 'http://soundcloud.com/joestrummer'})
person.name=u"哈哈"
js = json.dumps(person.to_primitive())
print(js)
f = person.validate() ##验证字段是否正确,如果不正确会报错,如果正确返回的就是none
print(f)
列表嵌套字典
你也可以嵌套其他的model层
from schematics.models import Model
from schematics.types import StringType, IntType
from schematics.types.compound import ListType,MultiType
class Person(Model):
mls = ListType(MultiType())
shi = StringType()
code = IntType()
mls_test = Person({'mls': [{
'en_US': 'Hello, world!',
'fr_FR': 'Bonjour tout le monde!',
'es_MX': '¡Hola, mundo!',
}],'shi':'kun','code':0})
print(mls_test.mls[0]["en_US"])
字典
from schematics.models import Model
from schematics.types import StringType, IntType,MultilingualStringType
class Person(Model):
mls = MultilingualStringType()
shi = StringType()
code = IntType()
mls_test = Person({'mls': {
'en_US': 'Hello, world!',
'fr_FR': 'Bonjour tout le monde!',
'es_MX': '¡Hola, mundo!',
},'shi':'kun','code':0})
print(mls_test.mls["en_US"])