diff --git a/django/utils/datastructures.py b/django/utils/datastructures.py index d0cd9085dd..ce2424065d 100644 --- a/django/utils/datastructures.py +++ b/django/utils/datastructures.py @@ -294,10 +294,19 @@ class MultiValueDict(dict): """Returns a list of (key, list) pairs.""" return super(MultiValueDict, self).items() + def iterlists(self): + """Yields (key, list) pairs.""" + return super(MultiValueDict, self).iteritems() + def values(self): """Returns a list of the last value on every key list.""" return [self[key] for key in self.keys()] - + + def itervalues(self): + """Yield the last value on every key list.""" + for key in self.iterkeys(): + yield self[key] + def copy(self): """Returns a copy of this object.""" return self.__deepcopy__() diff --git a/docs/ref/request-response.txt b/docs/ref/request-response.txt index cd0edc063c..6b29b3bb61 100644 --- a/docs/ref/request-response.txt +++ b/docs/ref/request-response.txt @@ -14,11 +14,11 @@ Django uses request and response objects to pass state through the system. When a page is requested, Django creates an :class:`HttpRequest` object that contains metadata about the request. Then Django loads the appropriate view, -passing the :class:`HttpRequest` as the first argument to the view function. Each -view is responsible for returning an :class:`HttpResponse` object. +passing the :class:`HttpRequest` as the first argument to the view function. +Each view is responsible for returning an :class:`HttpResponse` object. -This document explains the APIs for :class:`HttpRequest` and :class:`HttpResponse` -objects. +This document explains the APIs for :class:`HttpRequest` and +:class:`HttpResponse` objects. HttpRequest objects =================== @@ -103,7 +103,8 @@ All attributes except ``session`` should be considered read-only. * ``read(num_bytes=None)`` -- Read a number of bytes from the file. * ``name`` -- The name of the uploaded file. * ``size`` -- The size, in bytes, of the uploaded file. - * ``chunks(chunk_size=None)`` -- A generator that yields sequential chunks of data. + * ``chunks(chunk_size=None)`` -- A generator that yields sequential + chunks of data. See :ref:`topics-files` for more information. @@ -229,9 +230,10 @@ Methods .. versionadded:: 1.0 - Returns ``True`` if the request was made via an ``XMLHttpRequest``, by checking - the ``HTTP_X_REQUESTED_WITH`` header for the string ``'XMLHttpRequest'``. The - following major JavaScript libraries all send this header: + Returns ``True`` if the request was made via an ``XMLHttpRequest``, by + checking the ``HTTP_X_REQUESTED_WITH`` header for the string + ``'XMLHttpRequest'``. The following major JavaScript libraries all send this + header: * jQuery * Dojo @@ -317,6 +319,17 @@ a subclass of dictionary. Exceptions are outlined here: >>> q = QueryDict('a=1&a=2&a=3') >>> q.items() [('a', '3')] + +.. method:: QueryDict.iteritems() + + Just like the standard dictionary ``iteritems()`` method. Like + :meth:`QueryDict.items()` this uses the same last-value logic as + :meth:`QueryDict.__getitem()__`. + +.. method:: QueryDict.iterlists() + + Like :meth:`QueryDict.iteritems()` except it includes all values, as a list, + for each member of the dictionary. .. method:: QueryDict.values() @@ -327,6 +340,10 @@ a subclass of dictionary. Exceptions are outlined here: >>> q.values() ['3'] +.. method:: QueryDict.itervalues() + + Just like :meth:`QueryDict.values()`, except an iterator. + In addition, ``QueryDict`` has the following methods: .. method:: QueryDict.copy() diff --git a/tests/regressiontests/datastructures/tests.py b/tests/regressiontests/datastructures/tests.py index 9404a0a792..e658f3302e 100644 --- a/tests/regressiontests/datastructures/tests.py +++ b/tests/regressiontests/datastructures/tests.py @@ -45,6 +45,8 @@ MergeDict can merge MultiValueDicts ['Adrian', 'Simon'] >>> list(d.iteritems()) [('position', 'Developer'), ('name', 'Simon')] +>>> list(d.iterlists()) +[('position', ['Developer']), ('name', ['Adrian', 'Simon'])] >>> d['lastname'] Traceback (most recent call last): ... @@ -58,6 +60,10 @@ MultiValueDictKeyError: "Key 'lastname' not found in >> d.setlist('lastname', ['Holovaty', 'Willison']) >>> d.getlist('lastname') ['Holovaty', 'Willison'] +>>> d.values() +['Developer', 'Simon', 'Willison'] +>>> list(d.itervalues()) +['Developer', 'Simon', 'Willison'] ### SortedDict #################################################################