Code

Ticket #8278: 8278.querydict.2.diff

File 8278.querydict.2.diff, 1.7 KB (added by julien, 6 years ago)

2nd attempt. All tests pass.

Line 
1Index: django/django/http/__init__.py
2===================================================================
3--- django/django/http/__init__.py      (revision 8335)
4+++ django/django/http/__init__.py      (working copy)
5@@ -191,8 +191,13 @@
6     def update(self, other_dict):
7         self._assert_mutable()
8         f = lambda s: str_to_unicode(s, self.encoding)
9-        d = dict([(f(k), f(v)) for k, v in other_dict.items()])
10-        MultiValueDict.update(self, d)
11+        try:
12+            for key, _list in other_dict.lists():
13+                for value in _list:
14+                    MultiValueDict.update(self, { f(key): f(value) })
15+        except AttributeError:
16+            d = dict([(f(k), f(v)) for k, v in other_dict.items()])
17+            MultiValueDict.update(self, d)
18 
19     def pop(self, key, *args):
20         self._assert_mutable()
21Index: django/tests/regressiontests/httpwrappers/tests.py
22===================================================================
23--- django/tests/regressiontests/httpwrappers/tests.py  (revision 8335)
24+++ django/tests/regressiontests/httpwrappers/tests.py  (working copy)
25@@ -426,7 +426,19 @@
26 Traceback (most recent call last):
27 ...
28 UnicodeEncodeError: ..., HTTP response headers must be in US-ASCII format
29-
30+
31+
32+
33+############################################################# 
34+# Merging 2 QueryDict's with same keys and multiple values  # 
35+#############################################################
36+
37+>>> x = QueryDict('a=1&a=2', mutable=True)
38+>>> y = QueryDict('a=3&a=4')
39+>>> x.update(y)
40+>>> x
41+<QueryDict: {u'a': [u'1', u'2', u'3', u'4']}>
42+
43 """
44 
45 from django.http import QueryDict, HttpResponse