aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTom Christie2014-09-25 10:49:25 +0100
committerTom Christie2014-09-25 10:49:25 +0100
commit1420c76453c37c023a901dd0938d717b7b5e52ca (patch)
tree56170487538675a02bba7dc5864d5a8fee3651fd
parentfb1546ee50953faae8af505a0c90da00ac08ad92 (diff)
downloaddjango-rest-framework-1420c76453c37c023a901dd0938d717b7b5e52ca.tar.bz2
Ensure proper sorting of 'choices' attribute on ChoiceField
-rw-r--r--rest_framework/fields.py9
-rw-r--r--tests/test_fields.py4
-rw-r--r--tests/test_metadata.py4
3 files changed, 10 insertions, 7 deletions
diff --git a/rest_framework/fields.py b/rest_framework/fields.py
index 9280ea3a..d1aebbaf 100644
--- a/rest_framework/fields.py
+++ b/rest_framework/fields.py
@@ -2,6 +2,7 @@ from django.conf import settings
from django.core import validators
from django.core.exceptions import ValidationError
from django.utils import timezone
+from django.utils.datastructures import SortedDict
from django.utils.dateparse import parse_date, parse_datetime, parse_time
from django.utils.encoding import is_protected_type
from django.utils.translation import ugettext_lazy as _
@@ -166,7 +167,7 @@ class Field(object):
# my_field = serializer.CharField(source='my_field')
assert self._kwargs.get('source') != field_name, (
"It is redundant to specify `source='%s'` on field '%s' in "
- "serializer '%s', as it is the same the field name. "
+ "serializer '%s', because it is the same as the field name. "
"Remove the `source` keyword argument." %
(field_name, self.__class__.__name__, parent.__class__.__name__)
)
@@ -303,6 +304,7 @@ class BooleanField(Field):
'invalid': _('`{input}` is not a valid boolean.')
}
default_empty_html = False
+ initial = False
TRUE_VALUES = set(('t', 'T', 'true', 'True', 'TRUE', '1', 1, True))
FALSE_VALUES = set(('f', 'F', 'false', 'False', 'FALSE', '0', 0, 0.0, False))
@@ -365,6 +367,7 @@ class CharField(Field):
'blank': _('This field may not be blank.')
}
default_empty_html = ''
+ initial = ''
def __init__(self, **kwargs):
self.allow_blank = kwargs.pop('allow_blank', False)
@@ -775,9 +778,9 @@ class ChoiceField(Field):
for item in choices
]
if all(pairs):
- self.choices = dict([(key, display_value) for key, display_value in choices])
+ self.choices = SortedDict([(key, display_value) for key, display_value in choices])
else:
- self.choices = dict([(item, item) for item in choices])
+ self.choices = SortedDict([(item, item) for item in choices])
# Map the string representation of choices to the underlying value.
# Allows us to deal with eg. integer choices while supporting either
diff --git a/tests/test_fields.py b/tests/test_fields.py
index c2e03023..ebb88d3d 100644
--- a/tests/test_fields.py
+++ b/tests/test_fields.py
@@ -76,8 +76,8 @@ class TestFieldOptions:
ExampleSerializer()
assert str(exc_info.value) == (
"It is redundant to specify `source='example_field'` on field "
- "'CharField' in serializer 'ExampleSerializer', as it is the "
- "same the field name. Remove the `source` keyword argument."
+ "'CharField' in serializer 'ExampleSerializer', because it is the "
+ "same as the field name. Remove the `source` keyword argument."
)
diff --git a/tests/test_metadata.py b/tests/test_metadata.py
index 0ebea935..5ff59c72 100644
--- a/tests/test_metadata.py
+++ b/tests/test_metadata.py
@@ -84,9 +84,9 @@ class TestMetadata:
'read_only': False,
'label': 'Choice field',
'choices': [
- {'display_name': 'blue', 'value': 'blue'},
+ {'display_name': 'red', 'value': 'red'},
{'display_name': 'green', 'value': 'green'},
- {'display_name': 'red', 'value': 'red'}
+ {'display_name': 'blue', 'value': 'blue'}
]
},
'integer_field': {