Code

Ticket #12820: mergedict_patch.diff

File mergedict_patch.diff, 1.3 KB (added by sztamas, 4 years ago)
Line 
1Index: django/utils/datastructures.py
2===================================================================
3--- django/utils/datastructures.py      (revision 12381)
4+++ django/utils/datastructures.py      (working copy)
5@@ -1,3 +1,4 @@
6+import itertools
7 from types import GeneratorType
8 
9 from django.utils.copycompat import deepcopy
10@@ -37,6 +38,13 @@
11                 return dict_.getlist(key)
12         return []
13 
14+    def __iter__(self):
15+        seen = set()
16+        for key in itertools.chain.from_iterable(self.dicts):
17+            if key not in seen:
18+                seen.add(key)
19+                yield key
20+
21     def items(self):
22         item_list = []
23         for dict_ in self.dicts:
24Index: tests/regressiontests/datastructures/tests.py
25===================================================================
26--- tests/regressiontests/datastructures/tests.py       (revision 12381)
27+++ tests/regressiontests/datastructures/tests.py       (working copy)
28@@ -34,6 +34,10 @@
29 >>> mm.getlist('undefined')
30 []
31 
32+MergeDict supports iterating on keys
33+>>> [(k,mm.getlist(k)) for k in mm]
34+[('key2', ['value2', 'value3']), ('key1', ['value1']), ('key4', ['value5', 'value6'])]
35+
36 ### MultiValueDict ##########################################################
37 
38 >>> d = MultiValueDict({'name': ['Adrian', 'Simon'], 'position': ['Developer']})