Skip to content

Commit 6e3ba20

Browse files
committed
Merge pull request #2690 from delinhabit/hyperlinked-relation-callable-source
Support source='some_method' for HyperlinkedRelatedField.
2 parents c5a04a8 + d4353cc commit 6e3ba20

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

rest_framework/relations.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
from django.utils.translation import ugettext_lazy as _
1414

1515
from rest_framework.compat import OrderedDict
16-
from rest_framework.fields import Field, empty, get_attribute
16+
from rest_framework.fields import (
17+
Field, empty, get_attribute, is_simple_callable
18+
)
1719
from rest_framework.reverse import reverse
1820
from rest_framework.utils import html
1921

@@ -106,7 +108,12 @@ def get_attribute(self, instance):
106108
# Optimized case, return a mock object only containing the pk attribute.
107109
try:
108110
instance = get_attribute(instance, self.source_attrs[:-1])
109-
return PKOnlyObject(pk=instance.serializable_value(self.source_attrs[-1]))
111+
value = instance.serializable_value(self.source_attrs[-1])
112+
if is_simple_callable(value):
113+
# Handle edge case where the relationship `source` argument
114+
# points to a `get_relationship()` method on the model
115+
value = value().pk
116+
return PKOnlyObject(pk=value)
110117
except AttributeError:
111118
pass
112119

0 commit comments

Comments
 (0)