From f1a2eeb818366b11c44c86d2ccfa1d6822d216b9 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Wed, 10 Aug 2016 16:38:59 +0100 Subject: [PATCH] .choices property of RelatedField should preserve non-string values. (#4379) Update RelatedField.choices to support non-string values --- rest_framework/relations.py | 2 +- tests/test_model_serializer.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/rest_framework/relations.py b/rest_framework/relations.py index ad74d1f35..4b6b3bea4 100644 --- a/rest_framework/relations.py +++ b/rest_framework/relations.py @@ -168,7 +168,7 @@ class RelatedField(Field): return OrderedDict([ ( - six.text_type(self.to_representation(item)), + self.to_representation(item), self.display_value(item) ) for item in queryset diff --git a/tests/test_model_serializer.py b/tests/test_model_serializer.py index a14972f04..01243ff6e 100644 --- a/tests/test_model_serializer.py +++ b/tests/test_model_serializer.py @@ -614,7 +614,7 @@ class TestRelationalFieldDisplayValue(TestCase): fields = '__all__' serializer = TestSerializer() - expected = OrderedDict([('1', 'Red Color'), ('2', 'Yellow Color'), ('3', 'Green Color')]) + expected = OrderedDict([(1, 'Red Color'), (2, 'Yellow Color'), (3, 'Green Color')]) self.assertEqual(serializer.fields['color'].choices, expected) def test_custom_display_value(self): @@ -630,7 +630,7 @@ class TestRelationalFieldDisplayValue(TestCase): fields = '__all__' serializer = TestSerializer() - expected = OrderedDict([('1', 'My Red Color'), ('2', 'My Yellow Color'), ('3', 'My Green Color')]) + expected = OrderedDict([(1, 'My Red Color'), (2, 'My Yellow Color'), (3, 'My Green Color')]) self.assertEqual(serializer.fields['color'].choices, expected)