Skip to content

Commit

Permalink
Improve HTML form rendering of choice fields
Browse files Browse the repository at this point in the history
Expose basic value on BoundField instances and use it to improve
rendering of choice fields in HTML form output.

Fixes encode#4120, encode#4121
  • Loading branch information
arkadini committed May 21, 2016
1 parent f742452 commit a6c5cc7
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
{% elif select.end_option_group %}
</optgroup>
{% else %}
<option value="{{ select.value }}" {% if select.value == field.value %}selected{% endif %} {% if select.disabled %}disabled{% endif %}>{{ select.display_text }}</option>
<option value="{{ select.value }}" {% if select.value == field.basic_value %}selected{% endif %} {% if select.disabled %}disabled{% endif %}>{{ select.display_text }}</option>
{% endif %}
{% endfor %}
</select>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
{% elif select.end_option_group %}
</optgroup>
{% else %}
<option value="{{ select.value }}" {% if select.value in field.value %}selected{% endif %} {% if select.disabled %}disabled{% endif %}>{{ select.display_text }}</option>
<option value="{{ select.value }}" {% if select.value in field.basic_value %}selected{% endif %} {% if select.disabled %}disabled{% endif %}>{{ select.display_text }}</option>
{% endif %}
{% empty %}
<option>{{ no_items }}</option>
Expand Down
2 changes: 1 addition & 1 deletion rest_framework/templates/rest_framework/inline/select.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
{% elif select.end_option_group %}
</optgroup>
{% else %}
<option value="{{ select.value }}" {% if select.value == field.value %}selected{% endif %} {% if select.disabled %}disabled{% endif %}>{{ select.display_text }}</option>
<option value="{{ select.value }}" {% if select.value == field.basic_value %}selected{% endif %} {% if select.disabled %}disabled{% endif %}>{{ select.display_text }}</option>
{% endif %}
{% endfor %}
</select>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
{% elif select.end_option_group %}
</optgroup>
{% else %}
<option value="{{ select.value }}" {% if select.value in field.value %}selected{% endif %} {% if select.disabled %}disabled{% endif %}>{{ select.display_text }}</option>
<option value="{{ select.value }}" {% if select.value in field.basic_value %}selected{% endif %} {% if select.disabled %}disabled{% endif %}>{{ select.display_text }}</option>
{% endif %}
{% empty %}
<option>{{ no_items }}</option>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
{% elif select.end_option_group %}
</optgroup>
{% else %}
<option value="{{ select.value }}" {% if select.value == field.value %}selected{% endif %} {% if select.disabled %}disabled{% endif %}>{{ select.display_text }}</option>
<option value="{{ select.value }}" {% if select.value == field.basic_value %}selected{% endif %} {% if select.disabled %}disabled{% endif %}>{{ select.display_text }}</option>
{% endif %}
{% endfor %}
</select>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
{% elif select.end_option_group %}
</optgroup>
{% else %}
<option value="{{ select.value }}" {% if select.value in field.value %}selected{% endif %} {% if select.disabled %}disabled{% endif %}>{{ select.display_text }}</option>
<option value="{{ select.value }}" {% if select.value in field.basic_value %}selected{% endif %} {% if select.disabled %}disabled{% endif %}>{{ select.display_text }}</option>
{% endif %}
{% empty %}
<option>{{ no_items }}</option>
Expand Down
18 changes: 13 additions & 5 deletions rest_framework/utils/serializer_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
from rest_framework.compat import unicode_to_repr


# A singleton helper value to detect unset keyword arguments
unset = object()


class ReturnDict(OrderedDict):
"""
Return object from `serialier.data` for the `Serializer` class.
Expand Down Expand Up @@ -58,10 +62,11 @@ class BoundField(object):
providing an API similar to Django forms and form fields.
"""

def __init__(self, field, value, errors, prefix=''):
def __init__(self, field, value, errors, prefix='', basic_value=unset):
self._field = field
self._prefix = prefix
self.value = value
self.basic_value = value if basic_value is unset else basic_value
self.errors = errors
self.name = prefix + self.field_name

Expand All @@ -79,7 +84,8 @@ def __repr__(self):

def as_form_field(self):
value = '' if (self.value is None or self.value is False) else force_text(self.value)
return self.__class__(self._field, value, self.errors, self._prefix)
return self.__class__(self._field, value, self.errors, self._prefix,
self.basic_value)


class NestedBoundField(BoundField):
Expand All @@ -89,10 +95,11 @@ class NestedBoundField(BoundField):
`BoundField` that is used for serializer fields.
"""

def __init__(self, field, value, errors, prefix=''):
def __init__(self, field, value, errors, prefix='', basic_value=unset):
if value is None or value is '':
value = {}
super(NestedBoundField, self).__init__(field, value, errors, prefix)
super(NestedBoundField, self).__init__(field, value, errors, prefix,
basic_value)

def __iter__(self):
for field in self.fields.values():
Expand All @@ -113,7 +120,8 @@ def as_form_field(self):
values[key] = value
else:
values[key] = '' if (value is None or value is False) else force_text(value)
return self.__class__(self._field, values, self.errors, self._prefix)
return self.__class__(self._field, values, self.errors, self._prefix,
self.basic_value)


class BindingDict(collections.MutableMapping):
Expand Down

0 comments on commit a6c5cc7

Please sign in to comment.