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
09f22676
Commit
09f22676
authored
Oct 03, 2012
by
Tom Christie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Improve comments
parent
27c93c08
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
13 additions
and
14 deletions
+13
-14
rest_framework/fields.py
+13
-14
No files found.
rest_framework/fields.py
View file @
09f22676
...
...
@@ -7,7 +7,6 @@ from django.core import validators
from
django.core.exceptions
import
ValidationError
from
django.conf
import
settings
from
django.db
import
DEFAULT_DB_ALIAS
from
django.db.models.related
import
RelatedObject
from
django.utils.encoding
import
is_protected_type
,
smart_unicode
from
django.utils.translation
import
ugettext_lazy
as
_
from
rest_framework.compat
import
parse_date
,
parse_datetime
...
...
@@ -199,27 +198,23 @@ class RelatedField(Field):
class
PrimaryKeyRelatedField
(
RelatedField
):
"""
Serializes a
model related field or related manager
to a pk value.
Serializes a
related field or related object
to a pk value.
"""
# Note the we use ModelRelatedField's implementation, as we want to get the
# raw database value directly, since that won't involve another
# database lookup.
#
# An alternative implementation would simply be this...
#
# class PrimaryKeyRelatedField(RelatedField):
# def to_native(self, obj):
# return obj.pk
def
to_native
(
self
,
pk
):
"""
Simply returns the object's pk. You can subclass this method to
provide different serialization behavior of the pk.
(For example returning a URL based on the model's pk.)
You can subclass this method to provide different serialization
behavior based on the pk.
"""
return
pk
def
field_to_native
(
self
,
obj
,
field_name
):
# This is only implemented for performance reasons
#
# We could leave the default `RelatedField.field_to_native()` in place,
# and inside just implement `to_native()` as `return obj.pk`
#
# That would involve an extra database lookup.
try
:
pk
=
obj
.
serializable_value
(
self
.
source
or
field_name
)
except
AttributeError
:
...
...
@@ -235,6 +230,10 @@ class PrimaryKeyRelatedField(RelatedField):
class
ManyPrimaryKeyRelatedField
(
PrimaryKeyRelatedField
):
"""
Serializes a to-many related field or related manager to a pk value.
"""
def
field_to_native
(
self
,
obj
,
field_name
):
try
:
queryset
=
obj
.
serializable_value
(
self
.
source
or
field_name
)
...
...
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