Ticket #6580: 6580-unittests.diff

File 6580-unittests.diff, 2.8 KB (added by andrewebdev, 13 years ago)
  • docs/ref/request-response.txt

     
    448448    standard library. The copy will be mutable -- that is, you can change its
    449449    values.
    450450
    451 .. method:: QueryDict.getlist(key)
     451.. method:: QueryDict.getlist(key, default)
    452452
    453453    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.
    456457
    457458.. method:: QueryDict.setlist(key, list_)
    458459
  • django/utils/datastructures.py

     
    223223    A subclass of dictionary customized to handle multiple values for the
    224224    same key.
    225225
    226     >>> d = MultiValueDict({'name': ['Adrian', 'Simon'], 'position': ['Developer']})
    227     >>> d['name']
    228     'Simon'
    229     >>> d.getlist('name')
    230     ['Adrian', 'Simon']
    231     >>> d.get('lastname', 'nonexistent')
    232     'nonexistent'
    233     >>> d.setlist('lastname', ['Holovaty', 'Willison'])
    234 
    235226    This class exists to solve the irritating problem raised by cgi.parse_qs,
    236227    which returns a list for every key, even though most Web forms submit
    237228    single name-value pairs.
     
    300291            return default
    301292        return val
    302293
    303     def getlist(self, key):
     294    def getlist(self, key, default=None):
    304295        """
    305296        Returns the list of values for the passed key. If key doesn't exist,
    306         then an empty list is returned.
     297        then a default value is returned.
    307298        """
    308299        try:
    309300            return super(MultiValueDict, self).__getitem__(key)
    310301        except KeyError:
     302            if default is not None:
     303                return default
    311304            return []
    312305
    313306    def setlist(self, key, list_):
  • tests/regressiontests/utils/datastructures.py

     
    206206        self.assertEqual(d.get('lastname'), None)
    207207        self.assertEqual(d.get('lastname', 'nonexistent'), 'nonexistent')
    208208        self.assertEqual(d.getlist('lastname'), [])
     209        self.assertEqual(d.getlist('doesnotexist', ['Adrian', 'Simon']),
     210                         ['Adrian', 'Simon'])
    209211
    210212        d.setlist('lastname', ['Holovaty', 'Willison'])
    211213        self.assertEqual(d.getlist('lastname'), ['Holovaty', 'Willison'])
Back to Top