Author: jezdez
Date: 2011-06-16 09:34:55 -0700 (Thu, 16 Jun 2011)
New Revision: 16418

Modified:
   django/trunk/django/utils/datastructures.py
   django/trunk/docs/ref/request-response.txt
   django/trunk/tests/regressiontests/utils/datastructures.py
Log:
Fixed #12375 -- Added a dict() method to convert a MultiValueDict (such as a 
QueryDict) to a dictionary of key-value pairs. Thanks, oinopion and hvdklauw.

Modified: django/trunk/django/utils/datastructures.py
===================================================================
--- django/trunk/django/utils/datastructures.py 2011-06-16 16:34:46 UTC (rev 
16417)
+++ django/trunk/django/utils/datastructures.py 2011-06-16 16:34:55 UTC (rev 
16418)
@@ -391,6 +391,12 @@
         for key, value in kwargs.iteritems():
             self.setlistdefault(key, []).append(value)
 
+    def dict(self):
+        """
+        Returns current object as a dict with singular values.
+        """
+        return dict((key, self[key]) for key in self)
+
 class DotExpandedDict(dict):
     """
     A special dictionary constructor that takes a dictionary in which the keys

Modified: django/trunk/docs/ref/request-response.txt
===================================================================
--- django/trunk/docs/ref/request-response.txt  2011-06-16 16:34:46 UTC (rev 
16417)
+++ django/trunk/docs/ref/request-response.txt  2011-06-16 16:34:55 UTC (rev 
16418)
@@ -482,6 +482,18 @@
         >>> q.lists()
         [(u'a', [u'1', u'2', u'3'])]
 
+.. method:: QueryDict.dict()
+
+    .. versionadded:: 1.4
+
+    Returns ``dict`` representation of ``QueryDict``. For every (key, list)
+    pair in ``QueryDict``, ``dict`` will have (key, item), where item is one
+    element of the list, using same logic as :meth:`QueryDict.__getitem__()`::
+
+        >>> q = QueryDict('a=1&a=3&a=5')
+        >>> q.dict()
+        {u'a': u'5'}
+
 .. method:: QueryDict.urlencode([safe])
 
     Returns a string of the data in query-string format. Example::

Modified: django/trunk/tests/regressiontests/utils/datastructures.py
===================================================================
--- django/trunk/tests/regressiontests/utils/datastructures.py  2011-06-16 
16:34:46 UTC (rev 16417)
+++ django/trunk/tests/regressiontests/utils/datastructures.py  2011-06-16 
16:34:55 UTC (rev 16418)
@@ -235,7 +235,19 @@
             self.assertEqual(d1["key"], ["Penguin"])
             self.assertEqual(d2["key"], ["Penguin"])
 
+    def test_dict_translation(self):
+        mvd = MultiValueDict({
+            'devs': ['Bob', 'Joe'],
+            'pm': ['Rory'],
+        })
+        d = mvd.dict()
+        self.assertEqual(d.keys(), mvd.keys())
+        for key in mvd.keys():
+            self.assertEqual(d[key], mvd[key])
 
+        self.assertEqual({}, MultiValueDict().dict())
+
+
 class DotExpandedDictTests(DatastructuresTestCase):
 
     def test_dotexpandeddict(self):

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to django-updates@googlegroups.com.
To unsubscribe from this group, send email to 
django-updates+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to