【说站】python使用required定义必填字段
Python 使用 定义 required 必填
2023-06-13 09:13:15 时间
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电脑。
相关文章
- mt4 python_一个使用Python自动化交易外汇MT4脚本实现「建议收藏」
- python整除和取余写法_Python的整除和取余[通俗易懂]
- Python:类的定义与使用
- 长轮询的使用实现_python 轮询
- 【说站】python赋值运算符如何使用
- python输出unicode编码_Python以utf8编码读取文件
- 数据结构与算法Python_数据结构与算法python语言实现
- python使用opencv如何保存图片_OpenCV Python 保存图片
- python-Python与SQLite数据库-使用Python执行SQLite查询(一)
- python-Python与SQLite数据库-使用Python执行SQLite查询(二)
- python-Python与PostgreSQL数据库-使用Python执行PostgreSQL查询(一)
- Python使用platform库获取系统信息:操作系统信息、硬件信息、python环境信息
- Python变量的定义和使用
- 使用Python编程连接MySQL数据库(python连mysql)
- 使用Python操作MySQL数据库快速上手(python访问mysql数据库)
- Linux下使用Python开发体验之旅(linux使用python)
- 使用Python连接SQL Server数据库(python连接sqlserver数据库)
- Python玩转Redis:提升缓存效率(python使用redis)
- 使用Python连接SQL Server数据库(python连接sqlserver)
- 在Linux系统中使用Python编程语言(linux中python)
- 使用setup.py安装python包和卸载python包的方法
- linux系统使用python监测系统负载脚本分享
- 使用Python判断IP地址合法性的方法实例
- Python中的jqueryPyQuery库使用小结
- Python使用py2exe打包程序介绍