Skip to content

Commit b1c1867

Browse files
committed
Swapping to hassattr logic for pk attribute references in relations
1 parent 7ac3c3f commit b1c1867

File tree

1 file changed

+2
-4
lines changed

1 file changed

+2
-4
lines changed

rest_framework/relations.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,6 @@ def __init__(self, view_name=None, **kwargs):
166166
self.lookup_field = kwargs.pop('lookup_field', self.lookup_field)
167167
self.lookup_url_kwarg = kwargs.pop('lookup_url_kwarg', self.lookup_field)
168168
self.format = kwargs.pop('format', None)
169-
self.id_field = kwargs.pop('id_field', 'pk')
170169

171170
# We include this simply for dependency injection in tests.
172171
# We can't add it as a class attributes or it would expect an
@@ -197,7 +196,7 @@ def get_url(self, obj, view_name, request, format):
197196
attributes are not configured to correctly match the URL conf.
198197
"""
199198
# Unsaved objects will not yet have a valid URL.
200-
if getattr(obj, self.id_field) is None:
199+
if hasattr(obj, 'pk') and obj.pk is None:
201200
return None
202201

203202
lookup_value = getattr(obj, self.lookup_field)
@@ -341,7 +340,6 @@ def __init__(self, child_relation=None, *args, **kwargs):
341340
assert child_relation is not None, '`child_relation` is a required argument.'
342341
super(ManyRelatedField, self).__init__(*args, **kwargs)
343342
self.child_relation.bind(field_name='', parent=self)
344-
self.id_field = kwargs.pop('id_field', 'pk')
345343

346344
def get_value(self, dictionary):
347345
# We override the default field access in order to support
@@ -363,7 +361,7 @@ def to_internal_value(self, data):
363361

364362
def get_attribute(self, instance):
365363
# Can't have any relationships if not created
366-
if getattr(instance, self.id_field) is None:
364+
if hasattr(instance, 'pk') and instance.pk is None:
367365
return []
368366

369367
relationship = get_attribute(instance, self.source_attrs)

0 commit comments

Comments
 (0)