From 47a22a55cacf17264a54fbce7105ef6f501e0e56 Mon Sep 17 00:00:00 2001 From: Ion Scerbatiuc Date: Sat, 25 Jul 2015 09:44:00 -0700 Subject: [PATCH] Added a possible fix --- rest_framework/relations.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/rest_framework/relations.py b/rest_framework/relations.py index 97a6417ea..1d20ee82b 100644 --- a/rest_framework/relations.py +++ b/rest_framework/relations.py @@ -13,7 +13,9 @@ from django.utils.six.moves.urllib import parse as urlparse 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 @@ -106,7 +108,12 @@ class RelatedField(Field): # 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): + value = value().pk + + return PKOnlyObject(pk=value) except AttributeError: pass