小编G.B*_*sch的帖子

marshmallow - 序列化时如何将架构属性映射到另一个键?

我需要创建一个与给定规范一致的棉花糖架构,其中我无法更改键名。一个键是 Python 中的保留关键字“from”。

class TemporalExtentSchema(Schema):
    from = fields.String(required=True)
    to = fields.String(required=True)
Run Code Online (Sandbox Code Playgroud)

这在 Python 中当然是不允许的,所以我需要这样写:

class TemporalExtentSchema(Schema):
    t_from = fields.String(required=True)
    to = fields.String(required=True)
Run Code Online (Sandbox Code Playgroud)

我想要的是:

{
    "from": "2018-01-01",
    "to": "2018-01-10"
}
Run Code Online (Sandbox Code Playgroud)

序列化时是否可以将实例属性映射到另一个键(t_from -> from)?

python marshmallow

3
推荐指数
1
解决办法
2968
查看次数

标签 统计

marshmallow ×1

python ×1