尝试使用marshm用data_键加载数据时出现“缺少数据”

2024-04-29 10:02:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试使用python3.7上的marshmallow2.18.0来验证数据。我在等待json{'name': 'foo', 'emailAddress': 'x@x.org'},并用schema加载它:

class FooLoad(Schema):
    name = fields.Str()
    email = fields.Email(data_key='emailAddress', required=True)

除了加载上的数据键将返回给我类似{'name': 'foo', 'email': 'x@x.org'}的内容,但我在errors字段中得到了错误:

^{pr2}$

但是根据来自marshmallow docsdevDependencies或githubissue的示例,必须包含类似{'name': 'foo', 'email': 'x@x.org'}的数据。在

我想用与schema属性名不同的名称反序列化传入的日期(指定date_键上需要的内容),但是在尝试时遇到了错误。如何反序列化输入数据的名称,不同于schema属性,并在该属性的data_key字段中去极化?在


Tags: 数据keynameorg名称内容fieldsdata
1条回答
网友
1楼 · 发布于 2024-04-29 10:02:19

在棉花糖3中引入了{}。在

changelog entry

Backwards-incompatible: Add data_key parameter to fields for specifying the key in the input and output data dict. This parameter replaces both load_from and dump_to (#717).

和关联的pull-request。在

使用棉花糖2时,必须使用load_from/dump_to

class FooLoad(Schema):
    name = fields.Str()
    email = fields.Email(load_from='emailAddress', dump_to='emailAddress', required=True)

你用的是棉花糖2,但读的是棉花糖3的文档。在

注意,棉花糖3包含了一系列的改进,并且处于RC状态,所以如果你正在启动一个项目,你可以选择棉花糖3,为自己节省一些过渡工作在未来。在

相关问题 更多 >