Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #6580 -- Added `default` parameter to `MultiValueDict.getlist` …

…method (the base class for `QueryDict`). Many thanks to mk and andrewebdev.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@16260 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit fc8116cc4f5f27911c7685649ebac0a96fb39a49 1 parent 80d6089
Jannis Leidel authored
10  django/utils/datastructures.py
@@ -228,6 +228,10 @@ class MultiValueDict(dict):
228 228
     'Simon'
229 229
     >>> d.getlist('name')
230 230
     ['Adrian', 'Simon']
  231
+    >>> d.getlist('doesnotexist')
  232
+    []
  233
+    >>> d.getlist('doesnotexist', ['Adrian', 'Simon'])
  234
+    ['Adrian', 'Simon']
231 235
     >>> d.get('lastname', 'nonexistent')
232 236
     'nonexistent'
233 237
     >>> d.setlist('lastname', ['Holovaty', 'Willison'])
@@ -300,14 +304,16 @@ def get(self, key, default=None):
300 304
             return default
301 305
         return val
302 306
 
303  
-    def getlist(self, key):
  307
+    def getlist(self, key, default=None):
304 308
         """
305 309
         Returns the list of values for the passed key. If key doesn't exist,
306  
-        then an empty list is returned.
  310
+        then a default value is returned.
307 311
         """
308 312
         try:
309 313
             return super(MultiValueDict, self).__getitem__(key)
310 314
         except KeyError:
  315
+            if default is not None:
  316
+                return default
311 317
             return []
312 318
 
313 319
     def setlist(self, key, list_):
10  docs/ref/request-response.txt
@@ -448,11 +448,15 @@ In addition, ``QueryDict`` has the following methods:
448 448
     standard library. The copy will be mutable -- that is, you can change its
449 449
     values.
450 450
 
451  
-.. method:: QueryDict.getlist(key)
  451
+.. method:: QueryDict.getlist(key, default)
452 452
 
453 453
     Returns the data with the requested key, as a Python list. Returns an
454  
-    empty list if the key doesn't exist. It's guaranteed to return a list of
455  
-    some sort.
  454
+    empty list if the key doesn't exist and no default value was provided.
  455
+    It's guaranteed to return a list of some sort unless the default value
  456
+    was no list.
  457
+
  458
+    .. versionchanged:: 1.4
  459
+        The ``default`` parameter was added.
456 460
 
457 461
 .. method:: QueryDict.setlist(key, list_)
458 462
 
2  tests/regressiontests/utils/datastructures.py
@@ -206,6 +206,8 @@ def test_multivaluedict(self):
206 206
         self.assertEqual(d.get('lastname'), None)
207 207
         self.assertEqual(d.get('lastname', 'nonexistent'), 'nonexistent')
208 208
         self.assertEqual(d.getlist('lastname'), [])
  209
+        self.assertEqual(d.getlist('doesnotexist', ['Adrian', 'Simon']),
  210
+                         ['Adrian', 'Simon'])
209 211
 
210 212
         d.setlist('lastname', ['Holovaty', 'Willison'])
211 213
         self.assertEqual(d.getlist('lastname'), ['Holovaty', 'Willison'])

0 notes on commit fc8116c

Please sign in to comment.
Something went wrong with that request. Please try again.