question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support Werkzeug MultiDict in fields.List

See original GitHub issue

I’m talking about werkzeug.datastructures.ImmutableMultiDict. This is the type flask.request.values will give you. (actually it’s a CombinedMultiDict but they have the same interface).

You can get a single value out of it with [] or get(), or a list of values with getlist(). It would be neat if marshmallow’s fields.List() knew to check for the getlist() method, in case it is given a multidict that can return a list in this way.

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Reactions:4
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

14reactions
mindojo-victorcommented, Sep 30, 2017

@samjetski is correct that @sloria 's code doesn’t work for the issue.

This helped me:

class List(marshmallow.fields.List):
    def _deserialize(self, value, attr, data):
        if isinstance(data, dict) and hasattr(data, 'getlist'):
            value = data.getlist(attr)
        return super()._deserialize(value, attr, data)
1reaction
sloriacommented, Feb 1, 2015

Thanks for the suggestion. I’m going to hold off on adding built-in support for MultiDicts because it is a 3rd-party interface (albeit a very common one among the web frameworks). You can add support relatively easily by overriding a Schema's accessor:

from marshmallow import Schema, fields, pprint
from marshmallow.utils import get_value

from werkzeug.datastructures import ImmutableMultiDict

def multidict_aware(schema, key, obj, default=None):
    # Handle MultiDicts
    if isinstance(obj, dict) and hasattr(obj, 'getlist'):
        return obj.getlist(key)
    return get_value(key, obj, default)


class MySchema(Schema):
    __accessor__ = multidict_aware
    vals = fields.List(fields.Str)

d = ImmutableMultiDict([('vals', 'a'), ('vals', 'b')])
s = MySchema()
s.dump(d).data  # {'vals': ['a', 'b']}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Data Structures — Werkzeug Documentation (2.2.x)
Works like a regular MultiDict but preserves the order of the fields. To convert the ordered multi dict into a list you can...
Read more >
werkzeug.MultiDict — Flask API - GitHub Pages
MultiDict implements all standard dictionary methods. Internally, it saves all values for a key as a list, but the standard dict access methods...
Read more >
Advanced Usage - webargs 8.2.0 documentation
from webargs import fields from webargs.flaskparser import parser ... schema): # relies on the Flask (werkzeug) MultiDict type's implementation of # these ...
Read more >
Retrieving data from a combined multidict in python [duplicate]
from flask import Flask, request, jsonify from paho.mqtt import client as mqtt_client from werkzeug.datastructures import MultiDict app ...
Read more >
Werkzeug Documentation - Read the Docs
Werkzeug currently has experimental support for Python 3. ... For example MultiDict is a member of the werkzeug module but internally imple-.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found