python marshmallow如何提供默认值
发布时间:2024-07-21 20:24:09

说明
1、对于序列化和反序列化字段,marshmallow 它还提供了默认值,并且区分得非常清楚。例如 missing 反序列化时自动填充的数据,default 它是在序列化过程中自动填充的数据。
2、序列化和反序列化在没有真实值的情况下使用默认值。
实例
frommarshmallowimportSchema,fields
importdatetimeasdt
importuuid
classUserSchema(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电脑。
