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
This commit is contained in:
parent
80d6089029
commit
fc8116cc4f
|
@ -228,6 +228,10 @@ class MultiValueDict(dict):
|
||||||
'Simon'
|
'Simon'
|
||||||
>>> d.getlist('name')
|
>>> d.getlist('name')
|
||||||
['Adrian', 'Simon']
|
['Adrian', 'Simon']
|
||||||
|
>>> d.getlist('doesnotexist')
|
||||||
|
[]
|
||||||
|
>>> d.getlist('doesnotexist', ['Adrian', 'Simon'])
|
||||||
|
['Adrian', 'Simon']
|
||||||
>>> d.get('lastname', 'nonexistent')
|
>>> d.get('lastname', 'nonexistent')
|
||||||
'nonexistent'
|
'nonexistent'
|
||||||
>>> d.setlist('lastname', ['Holovaty', 'Willison'])
|
>>> d.setlist('lastname', ['Holovaty', 'Willison'])
|
||||||
|
@ -300,14 +304,16 @@ class MultiValueDict(dict):
|
||||||
return default
|
return default
|
||||||
return val
|
return val
|
||||||
|
|
||||||
def getlist(self, key):
|
def getlist(self, key, default=None):
|
||||||
"""
|
"""
|
||||||
Returns the list of values for the passed key. If key doesn't exist,
|
Returns the list of values for the passed key. If key doesn't exist,
|
||||||
then an empty list is returned.
|
then a default value is returned.
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
return super(MultiValueDict, self).__getitem__(key)
|
return super(MultiValueDict, self).__getitem__(key)
|
||||||
except KeyError:
|
except KeyError:
|
||||||
|
if default is not None:
|
||||||
|
return default
|
||||||
return []
|
return []
|
||||||
|
|
||||||
def setlist(self, key, list_):
|
def setlist(self, key, list_):
|
||||||
|
|
|
@ -448,11 +448,15 @@ In addition, ``QueryDict`` has the following methods:
|
||||||
standard library. The copy will be mutable -- that is, you can change its
|
standard library. The copy will be mutable -- that is, you can change its
|
||||||
values.
|
values.
|
||||||
|
|
||||||
.. method:: QueryDict.getlist(key)
|
.. method:: QueryDict.getlist(key, default)
|
||||||
|
|
||||||
Returns the data with the requested key, as a Python list. Returns an
|
Returns the data with the requested key, as a Python list. Returns an
|
||||||
empty list if the key doesn't exist. It's guaranteed to return a list of
|
empty list if the key doesn't exist and no default value was provided.
|
||||||
some sort.
|
It's guaranteed to return a list of some sort unless the default value
|
||||||
|
was no list.
|
||||||
|
|
||||||
|
.. versionchanged:: 1.4
|
||||||
|
The ``default`` parameter was added.
|
||||||
|
|
||||||
.. method:: QueryDict.setlist(key, list_)
|
.. method:: QueryDict.setlist(key, list_)
|
||||||
|
|
||||||
|
|
|
@ -206,6 +206,8 @@ class MultiValueDictTests(DatastructuresTestCase):
|
||||||
self.assertEqual(d.get('lastname'), None)
|
self.assertEqual(d.get('lastname'), None)
|
||||||
self.assertEqual(d.get('lastname', 'nonexistent'), 'nonexistent')
|
self.assertEqual(d.get('lastname', 'nonexistent'), 'nonexistent')
|
||||||
self.assertEqual(d.getlist('lastname'), [])
|
self.assertEqual(d.getlist('lastname'), [])
|
||||||
|
self.assertEqual(d.getlist('doesnotexist', ['Adrian', 'Simon']),
|
||||||
|
['Adrian', 'Simon'])
|
||||||
|
|
||||||
d.setlist('lastname', ['Holovaty', 'Willison'])
|
d.setlist('lastname', ['Holovaty', 'Willison'])
|
||||||
self.assertEqual(d.getlist('lastname'), ['Holovaty', 'Willison'])
|
self.assertEqual(d.getlist('lastname'), ['Holovaty', 'Willison'])
|
||||||
|
|
Loading…
Reference in New Issue