0
0
mirror of https://github.com/django/django.git synced 2024-12-01 15:42:04 +01:00

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."""
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__()

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
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()

View File

@ -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 <MultiValueDict: {'position
>>> d.setlist('lastname', ['Holovaty', 'Willison'])
>>> d.getlist('lastname')
['Holovaty', 'Willison']
>>> d.values()
['Developer', 'Simon', 'Willison']
>>> list(d.itervalues())
['Developer', 'Simon', 'Willison']
### SortedDict #################################################################