Allow JSONField webform-input to support JSON Objects

This commit is contained in:
pik 2016-05-21 11:48:16 +08:00
parent e35f0fd3c6
commit 75bb9c5b39
2 changed files with 16 additions and 0 deletions

View File

@ -1541,6 +1541,16 @@ class JSONField(Field):
self.binary = kwargs.pop('binary', False) self.binary = kwargs.pop('binary', False)
super(JSONField, self).__init__(*args, **kwargs) super(JSONField, self).__init__(*args, **kwargs)
def get_value(self, dictionary):
"""
If the incoming data is an html-input then it always comes in stringified via. the
multipart/form-input so we should call json.loads on it.
"""
ret = super(JSONField, self).get_value(dictionary)
if html.is_html_input(dictionary):
return json.loads(ret)
return ret
def to_internal_value(self, data): def to_internal_value(self, data):
try: try:
if self.binary: if self.binary:

View File

@ -30,6 +30,7 @@ from rest_framework.settings import api_settings
from rest_framework.utils import encoders from rest_framework.utils import encoders
from rest_framework.utils.breadcrumbs import get_breadcrumbs from rest_framework.utils.breadcrumbs import get_breadcrumbs
from rest_framework.utils.field_mapping import ClassLookupDict from rest_framework.utils.field_mapping import ClassLookupDict
from rest_framework.fields import JSONField
def zero_as_none(value): def zero_as_none(value):
@ -319,9 +320,14 @@ class HTMLFormRenderer(BaseRenderer):
style['template_pack'] = parent_style.get('template_pack', self.template_pack) style['template_pack'] = parent_style.get('template_pack', self.template_pack)
style['renderer'] = self style['renderer'] = self
# If we are rendering a JSON Field we want to convert Python literals to Javascript literals
if issubclass(field._proxy_class, JSONField):
field.value = json.dumps(field.value)
# Get a clone of the field with text-only value representation. # Get a clone of the field with text-only value representation.
field = field.as_form_field() field = field.as_form_field()
if style.get('input_type') == 'datetime-local' and isinstance(field.value, six.text_type): if style.get('input_type') == 'datetime-local' and isinstance(field.value, six.text_type):
field.value = field.value.rstrip('Z') field.value = field.value.rstrip('Z')