diff options
| author | Jamie Matthews | 2012-11-20 09:41:36 +0000 |
|---|---|---|
| committer | Jamie Matthews | 2012-11-20 09:41:36 +0000 |
| commit | 68c397371c647e88270b8c9e9a6f5f610bbd3a2b (patch) | |
| tree | 2d9de201c2e6ca25e99e7a029fe5dc620b7db42f /rest_framework/tests/serializer.py | |
| parent | a44a94dd6ea2d9497264e267a0354cb684d398f6 (diff) | |
| download | django-rest-framework-68c397371c647e88270b8c9e9a6f5f610bbd3a2b.tar.bz2 | |
Fix related serializers with source argument that resolves to a callable
Diffstat (limited to 'rest_framework/tests/serializer.py')
| -rw-r--r-- | rest_framework/tests/serializer.py | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/rest_framework/tests/serializer.py b/rest_framework/tests/serializer.py index 814c2499..9ca4f002 100644 --- a/rest_framework/tests/serializer.py +++ b/rest_framework/tests/serializer.py @@ -488,6 +488,7 @@ class ManyRelatedTests(TestCase): title = serializers.CharField() comments = BlogPostCommentSerializer(source='blogpostcomment_set') + self.comment_serializer_class = BlogPostCommentSerializer self.serializer_class = BlogPostSerializer def test_reverse_relations(self): @@ -506,6 +507,23 @@ class ManyRelatedTests(TestCase): self.assertEqual(serializer.data, expected) + def test_callable_source(self): + post = BlogPost.objects.create(title="Test blog post") + post.blogpostcomment_set.create(text="I love this blog post") + + class ExtendedBlogPostSerializer(self.serializer_class): + first_comment = self.comment_serializer_class(source='get_first_comment') + + serializer = ExtendedBlogPostSerializer(post) + expected = { + 'title': 'Test blog post', + 'comments': [ + {'text': 'I love this blog post'} + ], + 'first_comment': {'text': 'I love this blog post'} + } + self.assertEqual(serializer.data, expected) + # Test for issue #324 class BlankFieldTests(TestCase): |
