Ticket #12375: 12375-mvd-dict.diff

File 12375-mvd-dict.diff, 2.2 KB (added by Tomek Paczkowski, 13 years ago)
  • django/utils/datastructures.py

    diff --git a/django/utils/datastructures.py b/django/utils/datastructures.py
    index b0b449c..46f705f 100644
    a b class MultiValueDict(dict):  
    391391        for key, value in kwargs.iteritems():
    392392            self.setlistdefault(key, []).append(value)
    393393
     394    def dict(self):
     395        """
     396        Returns current object as a dict with singular values.
     397        """
     398        return dict((key, self[key]) for key in self)
     399
    394400class DotExpandedDict(dict):
    395401    """
    396402    A special dictionary constructor that takes a dictionary in which the keys
  • docs/ref/request-response.txt

    diff --git a/docs/ref/request-response.txt b/docs/ref/request-response.txt
    index ff04e25..a023ed4 100644
    a b In addition, ``QueryDict`` has the following methods:  
    482482        >>> q.lists()
    483483        [(u'a', [u'1', u'2', u'3'])]
    484484
     485.. method:: QueryDict.dict()
     486
     487    Returns ``dict`` representation of ``QueryDict``. For every (key, list)
     488    pair in ``QueryDict``, ``dict`` will have (key, item), where item is one
     489    element of the list, using same logic as :meth:`QueryDict.__getitem__()`::
     490
     491        >>> q = QueryDict('a=1&a=3&a=5')
     492        >>> q.dict()
     493        {u'a': u'5'}
     494
     495    .. versionadded:: 1.4
     496
    485497.. method:: QueryDict.urlencode([safe])
    486498
    487499    Returns a string of the data in query-string format. Example::
  • tests/regressiontests/utils/datastructures.py

    diff --git a/tests/regressiontests/utils/datastructures.py b/tests/regressiontests/utils/datastructures.py
    index 2f1a47a..88b83f7 100644
    a b class MultiValueDictTests(DatastructuresTestCase):  
    235235            self.assertEqual(d1["key"], ["Penguin"])
    236236            self.assertEqual(d2["key"], ["Penguin"])
    237237
     238    def test_dict_translation(self):
     239        mvd = MultiValueDict({
     240            'devs': ['Bob', 'Joe'],
     241            'pm': ['Rory'],
     242        })
     243        d = mvd.dict()
     244        self.assertEqual(d.keys(), mvd.keys())
     245        for key in mvd.keys():
     246            self.assertEqual(d[key], mvd[key])
     247
     248        self.assertEqual({}, MultiValueDict().dict())
     249
    238250
    239251class DotExpandedDictTests(DatastructuresTestCase):
    240252
Back to Top