mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-22 09:36:49 +03:00
parent
1c9ad52cb6
commit
91fa8b923a
|
@ -86,10 +86,13 @@ class BoundField(object):
|
|||
class JSONBoundField(BoundField):
|
||||
def as_form_field(self):
|
||||
value = self.value
|
||||
try:
|
||||
value = json.dumps(self.value, sort_keys=True, indent=4)
|
||||
except (TypeError, ValueError):
|
||||
pass
|
||||
# When HTML form input is used and the input is not valid
|
||||
# value will be a JSONString, rather than a JSON primitive.
|
||||
if not getattr(value, 'is_json_string', False):
|
||||
try:
|
||||
value = json.dumps(self.value, sort_keys=True, indent=4)
|
||||
except (TypeError, ValueError):
|
||||
pass
|
||||
return self.__class__(self._field, value, self.errors, self._prefix)
|
||||
|
||||
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
from django.http import QueryDict
|
||||
|
||||
from rest_framework import serializers
|
||||
|
||||
|
||||
|
@ -160,3 +162,15 @@ class TestNestedBoundField:
|
|||
)
|
||||
rendered_packed = ''.join(rendered.split())
|
||||
assert rendered_packed == expected_packed
|
||||
|
||||
|
||||
class TestJSONBoundField:
|
||||
def test_as_form_fields(self):
|
||||
class TestSerializer(serializers.Serializer):
|
||||
json_field = serializers.JSONField()
|
||||
|
||||
data = QueryDict(mutable=True)
|
||||
data.update({'json_field': '{"some": ["json"}'})
|
||||
serializer = TestSerializer(data=data)
|
||||
assert serializer.is_valid() is False
|
||||
assert serializer['json_field'].as_form_field().value == '{"some": ["json"}'
|
||||
|
|
Loading…
Reference in New Issue
Block a user