- schematics代替model层的功能
- schematics自动的字段有验证功能
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)
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"])
from jsonschema import validate
schema = {
"type" : "object",
"properties" : {
"price" : {"type" : "number"},
"name" : {"type" : "string"},
"list":{"maxItems":2},
"address":{'regex':'bj'},
},
}
validate({"name" : "Eggs", "price" : "dfda",'list':[1,5],'address':'bj-jiuxianqiao'}, schema)