From bb09a18312ff896dfcc2bc56b40da588c58dfc38 Mon Sep 17 00:00:00 2001 From: Ryan P Kilby Date: Tue, 14 Jan 2020 20:23:36 -0800 Subject: [PATCH] Fix related field pk-only optimization for props --- rest_framework/relations.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/rest_framework/relations.py b/rest_framework/relations.py index 3a2a8fb4b..3cd46379d 100644 --- a/rest_framework/relations.py +++ b/rest_framework/relations.py @@ -175,8 +175,13 @@ class RelatedField(Field): value = attribute_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 + # points to a `get_relationship()` method on the model. + value = value() + + # Handle edge case where relationship `source` argument points + # to an instance instead of a pk (e.g., a `@property`). + value = getattr(value, 'pk', value) + return PKOnlyObject(pk=value) except AttributeError: pass