From 6b08e97b6af8114d1ad82fc53a9cdf58168a278e Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Tue, 14 Jul 2015 15:47:13 +0100 Subject: [PATCH] Form fields should use textual only value representations. Closes #3139. Closes #2416. Closes #2558. --- rest_framework/renderers.py | 3 +++ rest_framework/utils/serializer_helpers.py | 6 +++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/rest_framework/renderers.py b/rest_framework/renderers.py index 64646d58c..4263adcef 100644 --- a/rest_framework/renderers.py +++ b/rest_framework/renderers.py @@ -318,6 +318,9 @@ class HTMLFormRenderer(BaseRenderer): style['template_pack'] = parent_style.get('template_pack', self.template_pack) style['renderer'] = self + # Get a clone of the field with text-only value representation. + field = field.as_form_field() + if style.get('input_type') == 'datetime-local' and isinstance(field.value, six.text_type): field.value = field.value.rstrip('Z') diff --git a/rest_framework/utils/serializer_helpers.py b/rest_framework/utils/serializer_helpers.py index b751a3588..9d2f70c88 100644 --- a/rest_framework/utils/serializer_helpers.py +++ b/rest_framework/utils/serializer_helpers.py @@ -2,7 +2,7 @@ from __future__ import unicode_literals import collections -from rest_framework.compat import OrderedDict, unicode_to_repr +from rest_framework.compat import OrderedDict, force_text, unicode_to_repr class ReturnDict(OrderedDict): @@ -54,6 +54,7 @@ class BoundField(object): """ def __init__(self, field, value, errors, prefix=''): self._field = field + self._prefix = prefix self.value = value self.errors = errors self.name = prefix + self.field_name @@ -70,6 +71,9 @@ class BoundField(object): self.__class__.__name__, self.value, self.errors )) + def as_form_field(self): + return BoundField(self._field, force_text(self.value), self.errors, self._prefix) + class NestedBoundField(BoundField): """