diff --git a/rest_framework/tests/models.py b/rest_framework/tests/models.py index 415e4d062..1a0078e8b 100644 --- a/rest_framework/tests/models.py +++ b/rest_framework/tests/models.py @@ -122,6 +122,13 @@ class Person(RESTFrameworkModel): name = models.CharField(max_length=10) age = models.IntegerField(null=True, blank=True) + @property + def info(self): + return { + 'name': self.name, + 'age': self.age, + } + # Model for issue #324 class BlankFieldModel(RESTFrameworkModel): diff --git a/rest_framework/tests/serializer.py b/rest_framework/tests/serializer.py index 9febc9c8a..5d46ff68f 100644 --- a/rest_framework/tests/serializer.py +++ b/rest_framework/tests/serializer.py @@ -46,8 +46,11 @@ class ActionItemSerializer(serializers.ModelSerializer): class PersonSerializer(serializers.ModelSerializer): + info = serializers.Field(source='info') + class Meta: model = Person + fields = ('name', 'age', 'info') class BasicTests(TestCase): @@ -69,6 +72,9 @@ class BasicTests(TestCase): 'created': datetime.datetime(2012, 1, 1), 'sub_comment': 'And Merry Christmas!' } + self.person_data = {'name': 'dwight', 'age': 35} + self.person = Person(**self.person_data) + self.person.save() def test_empty(self): serializer = CommentSerializer() @@ -100,6 +106,13 @@ class BasicTests(TestCase): self.assertTrue(serializer.object is expected) self.assertEquals(serializer.data['sub_comment'], 'And Merry Christmas!') + def test_field_with_dictionary(self): + """ Make sure that dictionaries from fields are left intact + """ + serializer = PersonSerializer(instance=self.person) + expected = self.person_data + self.assertEquals(serializer.data['info'], expected) + class ValidationTests(TestCase): def setUp(self):