python marshmallow如何提供默认值

python marshmallow如何提供默认值

说明

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基础教程