Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support source='some_method' for HyperlinkedRelatedField. #2690

Merged
Merged
Changes from all 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
11 changes: 9 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,12 @@ 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]))
value = instance.serializable_value(self.source_attrs[-1])
if is_simple_callable(value):
# Handle edge case where the relationship `source` argument
# points to a `get_relationship()` method on the model
value = value().pk
return PKOnlyObject(pk=value)
except AttributeError:
pass

Expand Down