From 4d786f85714b9514d10ad6df8d7256afd87a2161 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ryan=20O=E2=80=99Hara?= Date: Mon, 15 Jan 2018 15:00:33 -0800 Subject: [PATCH] Fix replaced instance being used when pk-only optimization raises AttributeError --- rest_framework/relations.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rest_framework/relations.py b/rest_framework/relations.py index c87b9299a..6bd3e76f3 100644 --- a/rest_framework/relations.py +++ b/rest_framework/relations.py @@ -163,8 +163,8 @@ class RelatedField(Field): if self.use_pk_only_optimization() and self.source_attrs: # Optimized case, return a mock object only containing the pk attribute. try: - instance = get_attribute(instance, self.source_attrs[:-1]) - value = instance.serializable_value(self.source_attrs[-1]) + attribute_instance = get_attribute(instance, self.source_attrs[:-1]) + 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