summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--django/utils/datastructures.py9
-rw-r--r--tests/regressiontests/datastructures/tests.py13
2 files changed, 18 insertions, 4 deletions
diff --git a/django/utils/datastructures.py b/django/utils/datastructures.py
index 25e9421575..82d914000f 100644
--- a/django/utils/datastructures.py
+++ b/django/utils/datastructures.py
@@ -2,6 +2,9 @@ class MergeDict(object):
"""
A simple class for creating new "virtual" dictionaries that actually look
up values in more than one dictionary, passed in the constructor.
+
+ If a key appears in more than one of the passed in dictionaries, only the
+ first occurrence will be used.
"""
def __init__(self, *dicts):
self.dicts = dicts
@@ -25,11 +28,9 @@ class MergeDict(object):
def getlist(self, key):
for dict_ in self.dicts:
- try:
+ if key in dict_.keys():
return dict_.getlist(key)
- except KeyError:
- pass
- raise KeyError
+ return []
def items(self):
item_list = []
diff --git a/tests/regressiontests/datastructures/tests.py b/tests/regressiontests/datastructures/tests.py
index 3b0ccde257..b5dc5d171b 100644
--- a/tests/regressiontests/datastructures/tests.py
+++ b/tests/regressiontests/datastructures/tests.py
@@ -20,6 +20,19 @@
>>> md2['chris']
'cool'
+MergeDict can merge MultiValueDicts
+>>> multi1 = MultiValueDict({'key1': ['value1'], 'key2': ['value2', 'value3']})
+>>> multi2 = MultiValueDict({'key2': ['value4'], 'key4': ['value5', 'value6']})
+>>> mm = MergeDict(multi1, multi2)
+
+# Although 'key2' appears in both dictionaries, only the first value is used.
+>>> mm.getlist('key2')
+['value2', 'value3']
+>>> mm.getlist('key4')
+['value5', 'value6']
+>>> mm.getlist('undefined')
+[]
+
### MultiValueDict ##########################################################
>>> d = MultiValueDict({'name': ['Adrian', 'Simon'], 'position': ['Developer']})