Python (flask/ marshmallow)ValueError: too many values to unpack (expected 2)

Python (flask/ marshmallow)ValueError: too many values to unpack (expected 2)

我正在开发一个 Flask 项目,我正在使用 marshmallow 来验证用户输入。 下面是一段代码:

def create_user():
    in_data = request.get_json()
    data, errors = Userschema.load(in_data)
    if errors:
        return (errors), 400
    fname = data.get('fname')
    lname = data.get('lname')
    email = data.get('email')
    password = data.get('password')
    cpass = data.get('cpass')

当我删除 errors 部分时,代码可以完美运行。当我按原样 运行 时,出现以下错误:

builtins.ValueError

ValueError: too many values to unpack (expected 2)

Traceback (most recent call last)

File "/home/..project-details.../venv3/lib/python3.6/site-packages/flask/app.py", line 2000, in call

error = None

ctx.auto_pop(error)

def __call__(self, environ, start_response):
    """Shortcut for :attr:`wsgi_app`."""
    return self.wsgi_app(environ, start_response)


def __repr__(self):
    return '<%s %r>' % (
        self.__class__.__name__,
        self.name,

注意:var in_data 是一个字典。 有什么想法吗??

我建议你检查你的依赖版本。 根据 Marshmallow API reference、schema.load returns:

Changed in version 3.0.0b7: This method returns the deserialized data rather than a (data, errors) duple. A ValidationError is raised if invalid data are passed.

我怀疑 python 正试图将 dict(作为单个对象返回)解压缩为两个变量。引发异常是因为没有任何内容可以打包到 'errors' 变量中。下面重现了错误:

d = dict()
d['test'] = 10101
a, b = d
print("%s : %s" % (a, b))