在 Python REST API 框架 Eve 中,是否可以在模式中设置默认值?如何?
是的,这是可能的,只需使用 default
模式设置。来自documentation :
The default value for the field. When serving POST and PUT requests, missing fields will be assigned the configured default values.
It works also for types dict and list. The latter is restricted and works only for lists with schemas (list with a random number of elements and each element being a dict)
schema = {
# Simple default
'title': {
'type': 'string',
'default': 'M.'
},
# Default in a dict
'others': {
'type': 'dict',
'schema': {
'code': {
'type': 'integer',
'default': 100
}
}
},
# Default in a list of dicts
'mylist': {
'type': 'list',
'schema': {
'type': 'dict',
'schema': {
'name': {'type': 'string'},
'customer': {
'type': 'boolean',
'default': False
}
}
}
}
}
我是一名优秀的程序员,十分优秀!