小编GLR*_*GLR的帖子

Python棉花糖:字典验证错误

我对棉花糖很陌生,但我的问题是指处理类似 dict 的对象的问题。Marshmallow 文档中没有可行的示例。我在堆栈溢出原始问题中遇到了一个简单的例子,这是答案的原始代码,假设这应该很简单

from marshmallow import Schema, fields, post_load, pprint

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)
Run Code Online (Sandbox Code Playgroud)

但是,当我运行代码时,我得到以下 NoneType 值

`None`
Run Code Online (Sandbox Code Playgroud)

输入尚未编组。

我正在使用棉花糖 3.0.0b20 …

python orm validationerror marshmallow

5
推荐指数
1
解决办法
4377
查看次数

标签 统计

marshmallow ×1

orm ×1

python ×1

validationerror ×1