我有一个棉花糖架构,其中对象使用键来引用结构另一部分中字典中定义的对象。我想在反序列化对象时自动解析键。如何以惯用的方式在棉花糖中实现此效果?
目前的解决方法是手动解析所有引用,但这似乎很笨拙,因为棉花糖的声明性性质应该能够为我们自动完成。
请注意,当columns are declared as relationships“时,Marshmallow-SQLAlchemy支持这种(反)序列化,它会自动为我们执行此操作,但我想使用JSON数据来实现。
这里是我要实现的示例,fields.Relationship
尚不存在:
class UserSchema(Schema):
name = fields.String()
email = fields.Email()
friends = fields.Relationship('self', path="AddressBook.contacts", many=True)
class AddressBookSchema(Schema):
contacts = nested.Dict(keys=fields.String(), values=fields.Nested(UserSchema))
# ... create ``user`` ...
serialized_data = AddressBookSchema().dump(user)
pprint(serialized_data)
# "contacts": {
# "Steve": {
# "name": "Steve",
# "email": "steve@example.com",
# "friends": ["Mike"]
# },
# "Mike": {
# "name": "Mike",
# "email": "mike@example.com",
# "friends": []
# }
deserialized_data = UserSchema().load(result)
pprint(deserialized_data)
# "contacts": {
# "Steve": {
# "name": "Steve",
# "email": "steve@example.com",
# "friends": [ {"name": "Mike", "email": "mike@example.com"]
# },
# "Mike": {
# "name": "Mike",
# "email": "mike@example.com",
# "friends": []
# }
答案 0 :(得分:4)
为什么不使用post_load
钩子简单地传输中间数据:
class UserSchema(Schema):
name = fields.String()
email = fields.Email()
friends = fields.List(fields.String())
class AddressBookSchema(Schema):
contacts = fields.Dict(keys=fields.String(), values=fields.Nested(UserSchema))
@post_load
def trans_friends(self, item):
for name in item['contacts']:
item['contacts'][name]['friends'] = [item['contacts'][n] for n in item['contacts'][name]['friends']]
data = """
{
"contacts": {
"Steve": {
"name": "Steve",
"email": "steve@example.com",
"friends": ["Mike"]
},
"Mike": {
"name": "Mike",
"email": "mike@example.com",
"friends": []
}
}
}
"""
deserialized_data = AddressBookSchema().loads(data)
pprint(deserialized_data)
产量:
UnmarshalResult(data={'contacts': {'Steve': {'name': 'Steve', 'email': 'steve@example.com', 'friends': [{'name': 'Mike', 'email': 'mike@example.com', 'friends': []}]}, 'Mike': {'name': 'Mike', 'email': 'mike@example.com', 'friends': []}}}, errors={})