Returning the dict from parse_qs in FormParser fails on forms.

Use `QueryDict` instead to return a value that is compatible with forms.
This commit is contained in:
Alen Mujezinovic 2011-06-09 16:24:27 +01:00
parent cf637e886b
commit a848923a39
2 changed files with 29 additions and 1 deletions

View File

@ -11,6 +11,7 @@ We need a method to be able to:
and multipart/form-data. (eg also handle multipart/json)
"""
from django.http import QueryDict
from django.http.multipartparser import MultiPartParser as DjangoMultiPartParser
from django.utils import simplejson as json
from djangorestframework import status
@ -117,7 +118,7 @@ class FormParser(BaseParser):
`data` will be a :class:`QueryDict` containing all the form parameters.
`files` will always be :const:`None`.
"""
data = parse_qs(stream.read(), keep_blank_values=True)
data = QueryDict(stream.read())
return (data, None)

View File

@ -131,3 +131,30 @@
# self.assertEqual(data['key1'], 'val1')
# self.assertEqual(files['file1'].read(), 'blablabla')
from StringIO import StringIO
from cgi import parse_qs
from django import forms
from django.test import TestCase
from djangorestframework.parsers import FormParser
class Form(forms.Form):
field1 = forms.CharField(max_length=3)
field2 = forms.CharField()
class TestFormParser(TestCase):
def setUp(self):
self.string = "field1=abc&field2=defghijk"
def test_fail(self):
""" Demonstrate that `parse_qs` fails on forms """
data = parse_qs(self.string, keep_blank_values=True)
self.assertEqual(Form(data).is_valid(), False)
def test_parse(self):
""" Make sure the `QueryDict` works OK """
parser = FormParser(None)
stream = StringIO(self.string)
(data, files) = parser.parse(stream)
self.assertEqual(Form(data).is_valid(), True)