028-86922220
建站资讯

网站建设资讯

为你提供网站建设行业资讯、网站优化知识、主机域名邮箱、网站开发常见问题等。

创新互联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如何提供默认值
本文来源:http://whjierui.cn/article/cdipepg.html

其他资讯