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
f4edd925
Commit
f4edd925
authored
Oct 29, 2012
by
Tom Christie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Writable welated fields should return a model instance from .from_native(), not a pk
parent
cf77fd69
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
9 additions
and
3 deletions
+9
-3
rest_framework/fields.py
+9
-3
No files found.
rest_framework/fields.py
View file @
f4edd925
...
...
@@ -244,7 +244,7 @@ class RelatedField(WritableField):
return
value
=
data
.
get
(
field_name
)
into
[(
self
.
source
or
field_name
)
+
'_id'
]
=
self
.
from_native
(
value
)
into
[(
self
.
source
or
field_name
)]
=
self
.
from_native
(
value
)
class
ManyRelatedMixin
(
object
):
...
...
@@ -288,6 +288,12 @@ class PrimaryKeyRelatedField(RelatedField):
def
to_native
(
self
,
pk
):
return
pk
def
from_native
(
self
,
data
):
try
:
return
self
.
queryset
.
get
(
pk
=
data
)
except
ObjectDoesNotExist
:
raise
ValidationError
(
'Invalid hyperlink - object does not exist.'
)
def
field_to_native
(
self
,
obj
,
field_name
):
try
:
# Prefer obj.serializable_value for performance reasons
...
...
@@ -377,7 +383,7 @@ class HyperlinkedRelatedField(RelatedField):
# Try explicit primary key.
if
pk
is
not
None
:
return
pk
queryset
=
self
.
queryset
.
filter
(
pk
=
pk
)
# Next, try looking up by slug.
elif
slug
is
not
None
:
slug_field
=
self
.
get_slug_field
()
...
...
@@ -390,7 +396,7 @@ class HyperlinkedRelatedField(RelatedField):
obj
=
queryset
.
get
()
except
ObjectDoesNotExist
:
raise
ValidationError
(
'Invalid hyperlink - object does not exist.'
)
return
obj
.
pk
return
obj
class
ManyHyperlinkedRelatedField
(
ManyRelatedMixin
,
HyperlinkedRelatedField
):
...
...
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