aaug...@bcc190cf-cafb-0310-a4f2-bffc1f526a37  committed 3828de7

[1.3.X] Fixed #17634 -- Optimized the performance of MultiValueDict by using append instead of copy and by minimizing the number of dict lookups. Backport of r17464 from trunk.

  • Participants
  • Parent commits c0a2d8b
  • Branches releases/1.3.X

Comments (0)

Files changed (2)

File django/utils/

     def setdefault(self, key, default=None):
         if key not in self:
             self[key] = default
+            return default
         return self[key]
-    def setlistdefault(self, key, default_list=()):
+    def setlistdefault(self, key, default_list=None):
         if key not in self:
+            if default_list is None:
+                default_list = []
             self.setlist(key, default_list)
+            return default_list
         return self.getlist(key)
     def appendlist(self, key, value):
         """Appends an item to the internal list associated with key."""
-        self.setlistdefault(key, [])
-        super(MultiValueDict, self).__setitem__(key, self.getlist(key) + [value])
+        self.setlistdefault(key).append(value)
     def items(self):
             other_dict = args[0]
             if isinstance(other_dict, MultiValueDict):
                 for key, value_list in other_dict.lists():
-                    self.setlistdefault(key, []).extend(value_list)
+                    self.setlistdefault(key).extend(value_list)
                     for key, value in other_dict.items():
-                        self.setlistdefault(key, []).append(value)
+                        self.setlistdefault(key).append(value)
                 except TypeError:
                     raise ValueError("MultiValueDict.update() takes either a MultiValueDict or dictionary")
         for key, value in kwargs.iteritems():
-            self.setlistdefault(key, []).append(value)
+            self.setlistdefault(key).append(value)
 class DotExpandedDict(dict):

File tests/regressiontests/utils/

                           ['Developer', 'Simon', 'Willison'])
+    def test_appendlist(self):
+        d = MultiValueDict()
+        d.appendlist('name', 'Adrian')
+        d.appendlist('name', 'Simon')
+        self.assertEqual(d.getlist('name'), ['Adrian', 'Simon'])
     def test_copy(self):
         for copy_func in [copy, lambda d: d.copy()]:
             d1 = MultiValueDict({