创新互联Python教程:python marshmallow如何提供默认值

说明

1、对于序列化和反序列化字段,marshmallow 还提供了默认值,而且区分得非常清楚。如 missing 则是在反序列化时自动填充的数据,default 则是在序列化时自动填充的数据。

2、在没有真实值的情况下,序列化和反序列化都是用了默认值。

实例

from marshmallow import Schema, fields
import datetime as dt
import uuid
 
class UserSchema(Schema):
    id = fields.UUID(missing=uuid.uuid1)
    birthdate = fields.DateTime(default=dt.datetime(2017, 9, 29))
 
print(UserSchema().load({}))
print(UserSchema().dump({}))

以上就是python marshmallow提供默认值的方法,希望对大家有所帮助。更多Python学习指路:创新互联python教程

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


分享文章:创新互联Python教程:python marshmallow如何提供默认值
标题URL:http://xwwzsj.com/article/cdipepg.html

其他资讯

售后响应及时

7×24小时客服热线

数据备份

更安全、更高效、更稳定

价格公道精准

项目经理精准报价不弄虚作假

合作无风险

重合同讲信誉,无效全额退款