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
51d86a65
Commit
51d86a65
authored
Nov 07, 2014
by
Tom Christie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Support dotted source on relational fields
parent
3e878a32
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
8 additions
and
7 deletions
+8
-7
rest_framework/relations.py
+8
-7
No files found.
rest_framework/relations.py
View file @
51d86a65
...
@@ -56,8 +56,8 @@ class RelatedField(Field):
...
@@ -56,8 +56,8 @@ class RelatedField(Field):
queryset
=
queryset
.
all
()
queryset
=
queryset
.
all
()
return
queryset
return
queryset
def
get_iterable
(
self
,
instance
,
source
):
def
get_iterable
(
self
,
instance
,
source
_attrs
):
relationship
=
get_attribute
(
instance
,
[
source
]
)
relationship
=
get_attribute
(
instance
,
source_attrs
)
return
relationship
.
all
()
if
(
hasattr
(
relationship
,
'all'
))
else
relationship
return
relationship
.
all
()
if
(
hasattr
(
relationship
,
'all'
))
else
relationship
@property
@property
...
@@ -106,11 +106,12 @@ class PrimaryKeyRelatedField(RelatedField):
...
@@ -106,11 +106,12 @@ class PrimaryKeyRelatedField(RelatedField):
# the related object. We return this directly instead of returning the
# the related object. We return this directly instead of returning the
# object itself, which would require a database lookup.
# object itself, which would require a database lookup.
try
:
try
:
return
PKOnlyObject
(
pk
=
instance
.
serializable_value
(
self
.
source
))
instance
=
get_attribute
(
instance
,
self
.
source_attrs
[:
-
1
])
return
PKOnlyObject
(
pk
=
instance
.
serializable_value
(
self
.
source_attrs
[
-
1
]))
except
AttributeError
:
except
AttributeError
:
return
get_attribute
(
instance
,
[
self
.
source
]
)
return
get_attribute
(
instance
,
self
.
source_attrs
)
def
get_iterable
(
self
,
instance
,
source
):
def
get_iterable
(
self
,
instance
,
source
_attrs
):
# For consistency with `get_attribute` we're using `serializable_value()`
# For consistency with `get_attribute` we're using `serializable_value()`
# here. Typically there won't be any difference, but some custom field
# here. Typically there won't be any difference, but some custom field
# types might return a non-primative value for the pk otherwise.
# types might return a non-primative value for the pk otherwise.
...
@@ -119,7 +120,7 @@ class PrimaryKeyRelatedField(RelatedField):
...
@@ -119,7 +120,7 @@ class PrimaryKeyRelatedField(RelatedField):
# would be better in some case, but would actually end up with *more*
# would be better in some case, but would actually end up with *more*
# queries if the developer is using `prefetch_related` across the
# queries if the developer is using `prefetch_related` across the
# relationship.
# relationship.
relationship
=
super
(
PrimaryKeyRelatedField
,
self
)
.
get_iterable
(
instance
,
source
)
relationship
=
super
(
PrimaryKeyRelatedField
,
self
)
.
get_iterable
(
instance
,
source
_attrs
)
return
[
return
[
PKOnlyObject
(
pk
=
item
.
serializable_value
(
'pk'
))
PKOnlyObject
(
pk
=
item
.
serializable_value
(
'pk'
))
for
item
in
relationship
for
item
in
relationship
...
@@ -318,7 +319,7 @@ class ManyRelation(Field):
...
@@ -318,7 +319,7 @@ class ManyRelation(Field):
]
]
def
get_attribute
(
self
,
instance
):
def
get_attribute
(
self
,
instance
):
return
self
.
child_relation
.
get_iterable
(
instance
,
self
.
source
)
return
self
.
child_relation
.
get_iterable
(
instance
,
self
.
source
_attrs
)
def
to_representation
(
self
,
iterable
):
def
to_representation
(
self
,
iterable
):
return
[
return
[
...
...
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