What is the difference between QueryDict and MultiValueDict?

I was looking to convert dict to QueryDict in my django project. There are a couple of links for explanation ( Django: can I create a QueryDict from a dictionary? And How to change django QueryDict to Python Dict? ). This is my simple dictionary that I want to convert abc = {'a': 1, 'b':[1,2,3]} . I tried this approach:

 from django.http import QueryDict from django.utils.datastructures import MultiValueDict abc = { 'a': 1, 'b':[1,2,3]} mdict = MultiValueDict(abc) qdict = QueryDict(mdict) 

This is the error I get

 /usr/lib/python2.7/urlparse.pyc in parse_qsl(qs, keep_blank_values, strict_parsing) 407 Returns a list, as Gd intended. 408 """ 409 pairs = [s2 for s1 in qs.split('&') for s2 in s1.split(';')] 410 r = [] 411 for name_value in pairs: AttributeError: 'MultiValueDict' object has no attribute 'split' 

Why did it fail and how can I do it? What are the differences between MultiValueDict and QueryDict?

+4
source share
3 answers

MultiValueDict is a subclass of a dictionary that can handle multiple values ​​assigned to a key. Therefore, you must pass the values from the dict as list . here 1->[1] .

In the HttpRequest object, the GET and POST attributes are instances of django.http.QueryDict, a dictionary-like class configured to handle multiple values ​​for the same key. This is necessary because some elements of the HTML form, in particular, pass multiple values ​​for the same key.

QueryDicts for .POST and request.GET will be unchanged when accessed in the normal request / response loop. To get the modified version you need to use .copy ().

Then MultiValueDict can be converted to QueryDict as

 abc = { 'a': [1], 'b':[1,2,3]} mdict = MultiValueDict(abc) qdict = QueryDict('', mutable=True) qdict.update(mdict) >>>QueryDict: {u'a': [1], u'b': [1, 2, 3]}> >>>dict(qdict.iterlists()) {u'a': [1], u'b': [1, 2, 3]} >>>qdict.getlist('b') [1, 2, 3] 
+2
source

QueryDict is a specialized MultiValueDict class. The only significant difference between the two is that QueryDict is unchanged by default.

For a quote from QueryDict docstring: -

A specialized MultiValueDict representing a query string. QueryDict can be used to represent GET or POST data. These are subclasses of MultiValueDict, since the keys in such data can be repeated, for example, in data from a form with a field. By default, QueryDicts are immutable, although the copy () method will always return a mutable copy.

You can consult the code itself - QueryDict code

Regarding the initialization of a dict request from a multi-valued dict, QueryDict does not allow passing any dic to __init__ . Perhaps this is an oversight on their part.

+3
source

From the definition of django.http.request.QueryDict:

"" A specialized MultiValueDict representing the query string. QueryDict can be used to represent GET or POST data. These are subclasses of MultiValueDict, since the keys in such data can be repeated, for example, in data from a form with a field. By default, QueryDicts are immutable, although the copy () method will always return a mutable copy. Both keys and values ​​set in this class are converted from this encoding (Default DEFAULT_CHARSET) to unicode. ""

And from the definition of django.utils.datastructures.MultiValueDict:

"" A subclass of a dictionary configured to handle multiple values ​​for the same key.

d = MultiValueDict ({'name': ['Adrian', 'Simon'], 'position': ['Developer']}) d ['name'] "Simon" d.getlist ('name') ['Adrian ',' Simon '] d.getlist (' doesnotexist ') [] d.getlist (' donotexist ', [' Adrian ',' Simon ']) [' Adrian ',' Simon '] d.get (' lastname ' , 'nonexistent') "Non-existent" d.setlist ('lastname', ['Holovaty', 'Willison']) This class exists to solve the annoyance problem caused by cgi.parse_qs, which returns a list for each key, even if most web -forms send single name-value pairs. ""

So, being a child of the MultiValueDict class, QueryDict has the additional property of being immutable. And that is the main difference.

+1
source

All Articles