mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-01-23 15:54:16 +03:00
Merge pull request #2690 from delinhabit/hyperlinked-relation-callable-source
Support source='some_method' for HyperlinkedRelatedField.
This commit is contained in:
commit
6e3ba202af
|
@ -13,7 +13,9 @@ from django.utils.six.moves.urllib import parse as urlparse
|
||||||
from django.utils.translation import ugettext_lazy as _
|
from django.utils.translation import ugettext_lazy as _
|
||||||
|
|
||||||
from rest_framework.compat import OrderedDict
|
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.reverse import reverse
|
||||||
from rest_framework.utils import html
|
from rest_framework.utils import html
|
||||||
|
|
||||||
|
@ -106,7 +108,12 @@ class RelatedField(Field):
|
||||||
# Optimized case, return a mock object only containing the pk attribute.
|
# Optimized case, return a mock object only containing the pk attribute.
|
||||||
try:
|
try:
|
||||||
instance = get_attribute(instance, self.source_attrs[:-1])
|
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:
|
except AttributeError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user