【说站】python使用required定义必填字段
2024-12-23
38
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电脑。
更新于:25天前赞一波!3
相关文章
- 【说站】python数据结构堆的介绍
- 【说站】python参数调用的注意点
- 【说站】python Pandas读取数据文件的优点
- 【说站】python中in和is的区分
- 【说站】python异常中常见关键字
- 【说站】python os.path.join()函数的使用
- 【说站】python如何使用skimage包提取图像
- 【说站】python confusion_matrix()是什么
- 【说站】python中os.path.join()函数是什么
- 【说站】python中有哪些比较操作
- 【说站】python字符串的用法总结
- 【说站】python列表数据如何增加和删除
- 【说站】python解释器的多种使用
- 【说站】python多行注释的方法整理
- 【说站】python列表有哪些特点
- 【说站】Python继承的原理分析
- 【说站】Python中三种模块类型的介绍
- 【说站】python输入三个数字从小到大排序
- 【说站】python输入数字变成月份
- 【说站】Python类属性如何使用
文章评论
评论问答