Marshmallow 序列化与父字段嵌套

myd*_*mon 5 flask marshmallow

抱歉,如果之前有人问过这个问题,我实际上找不到解决方案或类似的问题(可能使用了错误的词语)。

我正在更新现有的 Flask API,该 API 使用 marshmallow 和 peewee 从我们无法控制的客户端接收数据(无法更改 JSON 数据格式)。

数据格式是这样的:

{
    "site_id": "0102931",
    "update_date": "2018/02/11-09:33:23",
    "updated_by": "chan1",
    "crc": "a82131cf232ff120aaf00001293f",
    "data": [{"num": 1,
              "id": "09213/12312/1",
              "chain": "chain2",
              "operator": "0000122",
              "op_name": "Fred",
              "oid": "12092109300293"
             },
             {"num": 2,
              "id": "09213/12312/2",
              "chain": "chain1",
              "operator": "0000021",
              "op_name": "Melissa",
              "oid": "8883390393"
             }]           
}
Run Code Online (Sandbox Code Playgroud)

我们对主块中的任何内容都不感兴趣,而是对 site_id 感兴趣,在反序列化以创建模型并存储数据时,必须将其复制到列表中的每个对象中。

这是 peeewee 中的模型:

class production_item(db.Model):
   site_id = TextField(null=False)
   id_prod = TextField(null=False)
   num = SmallIntegerField(null=False)
   chain = TextField(null=False)
   operator = TextField(null=False)
   operator_name = TextField(null=True)
   order_id = TextField(null=False)
Run Code Online (Sandbox Code Playgroud)

这是棉花糖模式:

class prodItemSchema(Schema):
    num=String(required=True)
    id=String(required=True)
    chain=String(required=True)
    operator=String(required=True)
    op_name=String(required=False, allow_none=True)
    oid=String(required=False, allow_none=True)
Run Code Online (Sandbox Code Playgroud)

我找不到使用 load() 方法和 prodItemSchema 的预加载/后加载装饰器从主结构传递 site-id 的方法,因此无法创建模型。另外,我希望 Marshmallow 为我验证整个结构,而不是像他们现在在代码中所做的那样,在资源和模式之间分两部分进行。

但在文档中找不到制作类似内容的方法,这可能吗?

inf*_*ith 7

在 marshmallow 中,可以在序列化之前通过在父方案上使用pre_dump装饰器来设置context来将值从父方案传递到其子方案。设置上下文后,可以使用函数字段从父级获取值。

class Parent(Schema):
    id = fields.String(required=True)
    data = fields.Nested('Child', many=True)

    @pre_dump
    def set_context(self, parent, **kwargs):
        self.context['site_id'] = parent['id']
        return data

class Child(Schema):
    site_id = fields.Function(inherit_from_parent)

def inherit_from_parent(child, context):
    child['site_id'] = context['site_id']
    return child
Run Code Online (Sandbox Code Playgroud)