Improved documentation for QueryDict.
This commit is contained in:
parent
48241ec9c4
commit
7f4e2ef1e9
|
@ -299,12 +299,19 @@ class HttpRequest(object):
|
||||||
|
|
||||||
class QueryDict(MultiValueDict):
|
class QueryDict(MultiValueDict):
|
||||||
"""
|
"""
|
||||||
A specialized MultiValueDict that takes a query string when initialized.
|
A specialized MultiValueDict which represents a query string.
|
||||||
This is immutable unless you create a copy of it.
|
|
||||||
|
|
||||||
Values retrieved from this class are converted from the given encoding
|
A QueryDict can be used to represent GET or POST data. It subclasses
|
||||||
|
MultiValueDict since keys in such data can be repeated, for instance
|
||||||
|
in the data from a form with a <select multiple> field.
|
||||||
|
|
||||||
|
By default QueryDicts are immutable, though the copy() method
|
||||||
|
will always return a mutable copy.
|
||||||
|
|
||||||
|
Both keys and values set on this class are converted from the given encoding
|
||||||
(DEFAULT_CHARSET by default) to unicode.
|
(DEFAULT_CHARSET by default) to unicode.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# These are both reset in __init__, but is specified here at the class
|
# These are both reset in __init__, but is specified here at the class
|
||||||
# level so that unpickling will have valid values
|
# level so that unpickling will have valid values
|
||||||
_mutable = True
|
_mutable = True
|
||||||
|
|
|
@ -339,22 +339,37 @@ QueryDict objects
|
||||||
|
|
||||||
.. class:: QueryDict
|
.. class:: QueryDict
|
||||||
|
|
||||||
In an :class:`HttpRequest` object, the ``GET`` and ``POST`` attributes are instances
|
In an :class:`HttpRequest` object, the ``GET`` and ``POST`` attributes are
|
||||||
of ``django.http.QueryDict``. :class:`QueryDict` is a dictionary-like
|
instances of ``django.http.QueryDict``, a dictionary-like class customized to
|
||||||
class customized to deal with multiple values for the same key. This is
|
deal with multiple values for the same key. This is necessary because some HTML
|
||||||
necessary because some HTML form elements, notably
|
form elements, notably ``<select multiple>``, pass multiple values for the same
|
||||||
``<select multiple="multiple">``, pass multiple values for the same key.
|
key.
|
||||||
|
|
||||||
``QueryDict`` instances are immutable, unless you create a ``copy()`` of them.
|
The ``QueryDict``\ s at ``request.POST`` and ``request.GET`` will be immutable
|
||||||
That means you can't change attributes of ``request.POST`` and ``request.GET``
|
when accessed in a normal request/response cycle. To get a mutable version you
|
||||||
directly.
|
need to use ``.copy()``.
|
||||||
|
|
||||||
Methods
|
Methods
|
||||||
-------
|
-------
|
||||||
|
|
||||||
:class:`QueryDict` implements all the standard dictionary methods, because it's
|
:class:`QueryDict` implements all the standard dictionary methods because it's
|
||||||
a subclass of dictionary. Exceptions are outlined here:
|
a subclass of dictionary. Exceptions are outlined here:
|
||||||
|
|
||||||
|
.. method:: QueryDict.__init__(query_string, mutable=False, encoding=None)
|
||||||
|
|
||||||
|
Instantiates a ``QueryDict`` object based on ``query_string``.
|
||||||
|
|
||||||
|
>>> QueryDict('a=1&a=2&c=3')
|
||||||
|
<QueryDict: {u'a': [u'1', u'2'], u'b': [u'1']}>
|
||||||
|
|
||||||
|
Most ``QueryDict``\ s you encounter, and in particular those at
|
||||||
|
``request.POST`` and ``request.GET``, will be immutable. If you are
|
||||||
|
instantiating one yourself, you can make it mutable by passing
|
||||||
|
``mutable=True`` to its ``__init__()``.
|
||||||
|
|
||||||
|
Strings for setting both keys and values will be converted from ``encoding``
|
||||||
|
to unicode. If encoding is not set, it defaults to :setting:`DEFAULT_CHARSET`.
|
||||||
|
|
||||||
.. method:: QueryDict.__getitem__(key)
|
.. method:: QueryDict.__getitem__(key)
|
||||||
|
|
||||||
Returns the value for the given key. If the key has more than one value,
|
Returns the value for the given key. If the key has more than one value,
|
||||||
|
@ -367,8 +382,8 @@ a subclass of dictionary. Exceptions are outlined here:
|
||||||
|
|
||||||
Sets the given key to ``[value]`` (a Python list whose single element is
|
Sets the given key to ``[value]`` (a Python list whose single element is
|
||||||
``value``). Note that this, as other dictionary functions that have side
|
``value``). Note that this, as other dictionary functions that have side
|
||||||
effects, can only be called on a mutable ``QueryDict`` (one that was created
|
effects, can only be called on a mutable ``QueryDict`` (such as one that
|
||||||
via ``copy()``).
|
was created via ``copy()``).
|
||||||
|
|
||||||
.. method:: QueryDict.__contains__(key)
|
.. method:: QueryDict.__contains__(key)
|
||||||
|
|
||||||
|
@ -391,8 +406,7 @@ a subclass of dictionary. Exceptions are outlined here:
|
||||||
dictionary ``update()`` method, except it *appends* to the current
|
dictionary ``update()`` method, except it *appends* to the current
|
||||||
dictionary items rather than replacing them. For example::
|
dictionary items rather than replacing them. For example::
|
||||||
|
|
||||||
>>> q = QueryDict('a=1')
|
>>> q = QueryDict('a=1', mutable=True)
|
||||||
>>> q = q.copy() # to make it mutable
|
|
||||||
>>> q.update({'a': '2'})
|
>>> q.update({'a': '2'})
|
||||||
>>> q.getlist('a')
|
>>> q.getlist('a')
|
||||||
['1', '2']
|
['1', '2']
|
||||||
|
@ -437,8 +451,7 @@ In addition, ``QueryDict`` has the following methods:
|
||||||
.. method:: QueryDict.copy()
|
.. method:: QueryDict.copy()
|
||||||
|
|
||||||
Returns a copy of the object, using ``copy.deepcopy()`` from the Python
|
Returns a copy of the object, using ``copy.deepcopy()`` from the Python
|
||||||
standard library. The copy will be mutable -- that is, you can change its
|
standard library. This copy will be mutable even if the original was not.
|
||||||
values.
|
|
||||||
|
|
||||||
.. method:: QueryDict.getlist(key, default)
|
.. method:: QueryDict.getlist(key, default)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue