Commits

Branko Vukelic committed 3df74b6

Allow query setter to handle both dicts and strings

  • Participants
  • Parent commits 7856567

Comments (0)

Files changed (2)

File tests/test_url_helper.py

         u.query = dict(foo=1)
         self.assertEqual(u.query['foo'], '1')
 
+    def test_query_setter_with_string(self):
+        u = UrlHelper('/foo')
+        u.query = 'foo=1&bar=2'
+        self.assertEqual(u.query['foo'], '1')
+        self.assertEqual(u.query['bar'], '2')
+
     def test_query_string_property(self):
         u = UrlHelper('/foo?foo=1&bar=2')
         self.assertEqual(u.query_string, 'foo=1&bar=2')

File url_tools/helper.py

 
     @query.setter
     def query(self, value):
-        if not hasattr(value, '__getitem__'):
-            raise ValueError(
-                'Query can only be set to mapping type (like dict)'
-            )
-        self.query_dict = QueryDict('', mutable=True)
-        self.update_query_data(**value)
+        if type(value) is dict:
+            self.query_dict = QueryDict('', mutable=True)
+            self.update_query_data(**value)
+        else:
+            self.query_dict = QueryDict(value, mutable=True)
 
     @property
     def query_string(self):