@@ -166,7 +166,6 @@ def __init__(self, view_name=None, **kwargs):
166
166
self .lookup_field = kwargs .pop ('lookup_field' , self .lookup_field )
167
167
self .lookup_url_kwarg = kwargs .pop ('lookup_url_kwarg' , self .lookup_field )
168
168
self .format = kwargs .pop ('format' , None )
169
- self .id_field = kwargs .pop ('id_field' , 'pk' )
170
169
171
170
# We include this simply for dependency injection in tests.
172
171
# 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):
197
196
attributes are not configured to correctly match the URL conf.
198
197
"""
199
198
# 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 :
201
200
return None
202
201
203
202
lookup_value = getattr (obj , self .lookup_field )
@@ -341,7 +340,6 @@ def __init__(self, child_relation=None, *args, **kwargs):
341
340
assert child_relation is not None , '`child_relation` is a required argument.'
342
341
super (ManyRelatedField , self ).__init__ (* args , ** kwargs )
343
342
self .child_relation .bind (field_name = '' , parent = self )
344
- self .id_field = kwargs .pop ('id_field' , 'pk' )
345
343
346
344
def get_value (self , dictionary ):
347
345
# We override the default field access in order to support
@@ -363,7 +361,7 @@ def to_internal_value(self, data):
363
361
364
362
def get_attribute (self , instance ):
365
363
# 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 :
367
365
return []
368
366
369
367
relationship = get_attribute (instance , self .source_attrs )
0 commit comments