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

痴心易碎 提交于 2020-01-02 16:23:11

问题


I am working on a Flask project and I am using marshmallow to validate user input. Below is a code snippet:

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')

When I eliminate the errors part, the code works perfectly. When I run it as it is, I get the following error:

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,

Note: The var in_data is a dict. Any ideas??


回答1:


I recommend you check your dependency versions. Per the 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.

I suspect python is trying to unpack the dict (returned as a singular object) into two variables. The exception is raised because there is nothing to pack into the 'errors' variable. The below reproduces the error:

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


来源:https://stackoverflow.com/questions/51817237/python-flask-marshmallowvalueerror-too-many-values-to-unpack-expected-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!