Skip to content
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions rest_framework/relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
from django.utils.translation import ugettext_lazy as _

from rest_framework.compat import OrderedDict
from rest_framework.fields import Field, empty, get_attribute
from rest_framework.fields import (
Field, empty, get_attribute, is_simple_callable
)
from rest_framework.reverse import reverse
from rest_framework.utils import html

Expand Down Expand Up @@ -106,7 +108,14 @@ def get_attribute(self, instance):
# Optimized case, return a mock object only containing the pk attribute.
try:
instance = get_attribute(instance, self.source_attrs[:-1])
return PKOnlyObject(pk=instance.serializable_value(self.source_attrs[-1]))

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could probably drop the extra whitespacing in this block (one line here)

# Handle edge case where the relationship `source` argument
# points to a `get_relationship()` method on the model
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment should probably go instead the "if" block.

value = instance.serializable_value(self.source_attrs[-1])
if is_simple_callable(value):
value = value().pk

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And this line here.

return PKOnlyObject(pk=value)
except AttributeError:
pass

Expand Down