From 7ac3c3fff7ae029a331ae0b6c149ac5ab97357fc Mon Sep 17 00:00:00 2001 From: Devon Bleibtrey Date: Mon, 23 Mar 2015 10:42:42 -0400 Subject: [PATCH] Added enhancement for pk reference in many=True relations --- rest_framework/relations.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/rest_framework/relations.py b/rest_framework/relations.py index 13668975c..7483a9390 100644 --- a/rest_framework/relations.py +++ b/rest_framework/relations.py @@ -341,6 +341,7 @@ class ManyRelatedField(Field): assert child_relation is not None, '`child_relation` is a required argument.' super(ManyRelatedField, self).__init__(*args, **kwargs) self.child_relation.bind(field_name='', parent=self) + self.id_field = kwargs.pop('id_field', 'pk') def get_value(self, dictionary): # We override the default field access in order to support @@ -362,7 +363,7 @@ class ManyRelatedField(Field): def get_attribute(self, instance): # Can't have any relationships if not created - if not instance.pk: + if getattr(instance, self.id_field) is None: return [] relationship = get_attribute(instance, self.source_attrs)