创新互联Python教程:python使用required定义必填字段

说明

在乌翠等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供成都网站建设、成都网站设计 网站设计制作按需开发,公司网站建设,企业网站建设,品牌网站设计,全网营销推广,外贸网站建设,乌翠网站建设费用合理。

1、要想定义必填字段,只需要在 fields 里面加入 required 参数并设置为 True 即可。

2、还可以自定义错误信息,使用 error_messages 即可。

实例

from pprint import pprint
from marshmallow import Schema, fields, ValidationError
 
class UserSchema(Schema):
    name = fields.String(required=True)
    age = fields.Integer(required=True, error_messages={'required': 'Age is required.'})
    city = fields.String(
        required=True,
        error_messages={'required': {'message': 'City required', 'code': 400}},
    )
    email = fields.Email()
 
try:
    result = UserSchema().load({'email': 'foo@bar.com'})
except ValidationError as err:
    pprint(err.messages)

以上就是python使用required定义必填字段,希望对大家有所帮助。更多Python学习指路:创新互联python教程

本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。


文章标题:创新互联Python教程:python使用required定义必填字段
当前URL:http://csdahua.cn/article/dhihpss.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流