Ticket #6580: 6580-unittests.diff
File 6580-unittests.diff, 2.8 KB (added by , 13 years ago) |
---|
-
docs/ref/request-response.txt
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. 456 457 457 458 .. method:: QueryDict.setlist(key, list_) 458 459 -
django/utils/datastructures.py
223 223 A subclass of dictionary customized to handle multiple values for the 224 224 same key. 225 225 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 235 226 This class exists to solve the irritating problem raised by cgi.parse_qs, 236 227 which returns a list for every key, even though most Web forms submit 237 228 single name-value pairs. … … 300 291 return default 301 292 return val 302 293 303 def getlist(self, key ):294 def getlist(self, key, default=None): 304 295 """ 305 296 Returns the list of values for the passed key. If key doesn't exist, 306 then a n empty listis returned.297 then a default value is returned. 307 298 """ 308 299 try: 309 300 return super(MultiValueDict, self).__getitem__(key) 310 301 except KeyError: 302 if default is not None: 303 return default 311 304 return [] 312 305 313 306 def setlist(self, key, list_): -
tests/regressiontests/utils/datastructures.py
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'])