mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-27 03:54:01 +03:00
Merge pull request #435 from j4mie/fix-serializer-source
Fix related serializers with source argument that resolves to a callable
This commit is contained in:
commit
d22daa04e6
|
@ -277,6 +277,9 @@ class BaseSerializer(Field):
|
||||||
"""
|
"""
|
||||||
obj = getattr(obj, self.source or field_name)
|
obj = getattr(obj, self.source or field_name)
|
||||||
|
|
||||||
|
if is_simple_callable(obj):
|
||||||
|
obj = obj()
|
||||||
|
|
||||||
# If the object has an "all" method, assume it's a relationship
|
# If the object has an "all" method, assume it's a relationship
|
||||||
if is_simple_callable(getattr(obj, 'all', None)):
|
if is_simple_callable(getattr(obj, 'all', None)):
|
||||||
return [self.to_native(item) for item in obj.all()]
|
return [self.to_native(item) for item in obj.all()]
|
||||||
|
|
|
@ -127,6 +127,9 @@ class ActionItem(RESTFrameworkModel):
|
||||||
class BlogPost(RESTFrameworkModel):
|
class BlogPost(RESTFrameworkModel):
|
||||||
title = models.CharField(max_length=100)
|
title = models.CharField(max_length=100)
|
||||||
|
|
||||||
|
def get_first_comment(self):
|
||||||
|
return self.blogpostcomment_set.all()[0]
|
||||||
|
|
||||||
|
|
||||||
class BlogPostComment(RESTFrameworkModel):
|
class BlogPostComment(RESTFrameworkModel):
|
||||||
text = models.TextField()
|
text = models.TextField()
|
||||||
|
|
|
@ -479,7 +479,10 @@ class CallableDefaultValueTests(TestCase):
|
||||||
|
|
||||||
|
|
||||||
class ManyRelatedTests(TestCase):
|
class ManyRelatedTests(TestCase):
|
||||||
def setUp(self):
|
def test_reverse_relations(self):
|
||||||
|
post = BlogPost.objects.create(title="Test blog post")
|
||||||
|
post.blogpostcomment_set.create(text="I hate this blog post")
|
||||||
|
post.blogpostcomment_set.create(text="I love this blog post")
|
||||||
|
|
||||||
class BlogPostCommentSerializer(serializers.Serializer):
|
class BlogPostCommentSerializer(serializers.Serializer):
|
||||||
text = serializers.CharField()
|
text = serializers.CharField()
|
||||||
|
@ -488,14 +491,7 @@ class ManyRelatedTests(TestCase):
|
||||||
title = serializers.CharField()
|
title = serializers.CharField()
|
||||||
comments = BlogPostCommentSerializer(source='blogpostcomment_set')
|
comments = BlogPostCommentSerializer(source='blogpostcomment_set')
|
||||||
|
|
||||||
self.serializer_class = BlogPostSerializer
|
serializer = BlogPostSerializer(instance=post)
|
||||||
|
|
||||||
def test_reverse_relations(self):
|
|
||||||
post = BlogPost.objects.create(title="Test blog post")
|
|
||||||
post.blogpostcomment_set.create(text="I hate this blog post")
|
|
||||||
post.blogpostcomment_set.create(text="I love this blog post")
|
|
||||||
|
|
||||||
serializer = self.serializer_class(instance=post)
|
|
||||||
expected = {
|
expected = {
|
||||||
'title': 'Test blog post',
|
'title': 'Test blog post',
|
||||||
'comments': [
|
'comments': [
|
||||||
|
@ -506,6 +502,25 @@ class ManyRelatedTests(TestCase):
|
||||||
|
|
||||||
self.assertEqual(serializer.data, expected)
|
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 BlogPostCommentSerializer(serializers.Serializer):
|
||||||
|
text = serializers.CharField()
|
||||||
|
|
||||||
|
class BlogPostSerializer(serializers.Serializer):
|
||||||
|
title = serializers.CharField()
|
||||||
|
first_comment = BlogPostCommentSerializer(source='get_first_comment')
|
||||||
|
|
||||||
|
serializer = BlogPostSerializer(post)
|
||||||
|
|
||||||
|
expected = {
|
||||||
|
'title': 'Test blog post',
|
||||||
|
'first_comment': {'text': 'I love this blog post'}
|
||||||
|
}
|
||||||
|
self.assertEqual(serializer.data, expected)
|
||||||
|
|
||||||
|
|
||||||
# Test for issue #324
|
# Test for issue #324
|
||||||
class BlankFieldTests(TestCase):
|
class BlankFieldTests(TestCase):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user