Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
django-rest-framework
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
django-rest-framework
Commits
68c39737
Commit
68c39737
authored
Nov 20, 2012
by
Jamie Matthews
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix related serializers with source argument that resolves to a callable
parent
a44a94dd
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
24 additions
and
0 deletions
+24
-0
rest_framework/serializers.py
+3
-0
rest_framework/tests/models.py
+3
-0
rest_framework/tests/serializer.py
+18
-0
No files found.
rest_framework/serializers.py
View file @
68c39737
...
...
@@ -277,6 +277,9 @@ class BaseSerializer(Field):
"""
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
is_simple_callable
(
getattr
(
obj
,
'all'
,
None
)):
return
[
self
.
to_native
(
item
)
for
item
in
obj
.
all
()]
...
...
rest_framework/tests/models.py
View file @
68c39737
...
...
@@ -127,6 +127,9 @@ class ActionItem(RESTFrameworkModel):
class
BlogPost
(
RESTFrameworkModel
):
title
=
models
.
CharField
(
max_length
=
100
)
def
get_first_comment
(
self
):
return
self
.
blogpostcomment_set
.
all
()[
0
]
class
BlogPostComment
(
RESTFrameworkModel
):
text
=
models
.
TextField
()
...
...
rest_framework/tests/serializer.py
View file @
68c39737
...
...
@@ -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
):
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment