Fixed #8847, #10370: added some missing methods to MultiValueDict after [8399]. Thanks, James Turk and rfk.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@10241 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Jacob Kaplan-Moss 2009-03-31 06:30:07 +00:00
parent 9ae873fcd8
commit 184ea1c91f
3 changed files with 41 additions and 9 deletions

View File

@ -294,10 +294,19 @@ class MultiValueDict(dict):
"""Returns a list of (key, list) pairs.""" """Returns a list of (key, list) pairs."""
return super(MultiValueDict, self).items() return super(MultiValueDict, self).items()
def iterlists(self):
"""Yields (key, list) pairs."""
return super(MultiValueDict, self).iteritems()
def values(self): def values(self):
"""Returns a list of the last value on every key list.""" """Returns a list of the last value on every key list."""
return [self[key] for key in self.keys()] 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): def copy(self):
"""Returns a copy of this object.""" """Returns a copy of this object."""
return self.__deepcopy__() return self.__deepcopy__()

View File

@ -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 When a page is requested, Django creates an :class:`HttpRequest` object that
contains metadata about the request. Then Django loads the appropriate view, contains metadata about the request. Then Django loads the appropriate view,
passing the :class:`HttpRequest` as the first argument to the view function. Each passing the :class:`HttpRequest` as the first argument to the view function.
view is responsible for returning an :class:`HttpResponse` object. Each view is responsible for returning an :class:`HttpResponse` object.
This document explains the APIs for :class:`HttpRequest` and :class:`HttpResponse` This document explains the APIs for :class:`HttpRequest` and
objects. :class:`HttpResponse` objects.
HttpRequest 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. * ``read(num_bytes=None)`` -- Read a number of bytes from the file.
* ``name`` -- The name of the uploaded file. * ``name`` -- The name of the uploaded file.
* ``size`` -- The size, in bytes, 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. See :ref:`topics-files` for more information.
@ -229,9 +230,10 @@ Methods
.. versionadded:: 1.0 .. versionadded:: 1.0
Returns ``True`` if the request was made via an ``XMLHttpRequest``, by checking Returns ``True`` if the request was made via an ``XMLHttpRequest``, by
the ``HTTP_X_REQUESTED_WITH`` header for the string ``'XMLHttpRequest'``. The checking the ``HTTP_X_REQUESTED_WITH`` header for the string
following major JavaScript libraries all send this header: ``'XMLHttpRequest'``. The following major JavaScript libraries all send this
header:
* jQuery * jQuery
* Dojo * Dojo
@ -317,6 +319,17 @@ a subclass of dictionary. Exceptions are outlined here:
>>> q = QueryDict('a=1&a=2&a=3') >>> q = QueryDict('a=1&a=2&a=3')
>>> q.items() >>> q.items()
[('a', '3')] [('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() .. method:: QueryDict.values()
@ -327,6 +340,10 @@ a subclass of dictionary. Exceptions are outlined here:
>>> q.values() >>> q.values()
['3'] ['3']
.. method:: QueryDict.itervalues()
Just like :meth:`QueryDict.values()`, except an iterator.
In addition, ``QueryDict`` has the following methods: In addition, ``QueryDict`` has the following methods:
.. method:: QueryDict.copy() .. method:: QueryDict.copy()

View File

@ -45,6 +45,8 @@ MergeDict can merge MultiValueDicts
['Adrian', 'Simon'] ['Adrian', 'Simon']
>>> list(d.iteritems()) >>> list(d.iteritems())
[('position', 'Developer'), ('name', 'Simon')] [('position', 'Developer'), ('name', 'Simon')]
>>> list(d.iterlists())
[('position', ['Developer']), ('name', ['Adrian', 'Simon'])]
>>> d['lastname'] >>> d['lastname']
Traceback (most recent call last): Traceback (most recent call last):
... ...
@ -58,6 +60,10 @@ MultiValueDictKeyError: "Key 'lastname' not found in <MultiValueDict: {'position
>>> d.setlist('lastname', ['Holovaty', 'Willison']) >>> d.setlist('lastname', ['Holovaty', 'Willison'])
>>> d.getlist('lastname') >>> d.getlist('lastname')
['Holovaty', 'Willison'] ['Holovaty', 'Willison']
>>> d.values()
['Developer', 'Simon', 'Willison']
>>> list(d.itervalues())
['Developer', 'Simon', 'Willison']
### SortedDict ################################################################# ### SortedDict #################################################################