File tree Expand file tree Collapse file tree 2 files changed +19
-3
lines changed Expand file tree Collapse file tree 2 files changed +19
-3
lines changed Original file line number Diff line number Diff line change @@ -163,8 +163,8 @@ def get_attribute(self, instance):
163
163
if self .use_pk_only_optimization () and self .source_attrs :
164
164
# Optimized case, return a mock object only containing the pk attribute.
165
165
try :
166
- instance = get_attribute (instance , self .source_attrs [:- 1 ])
167
- value = instance .serializable_value (self .source_attrs [- 1 ])
166
+ attribute_instance = get_attribute (instance , self .source_attrs [:- 1 ])
167
+ value = attribute_instance .serializable_value (self .source_attrs [- 1 ])
168
168
if is_simple_callable (value ):
169
169
# Handle edge case where the relationship `source` argument
170
170
# points to a `get_relationship()` method on the model
Original file line number Diff line number Diff line change 3
3
import pytest
4
4
from _pytest .monkeypatch import MonkeyPatch
5
5
from django .conf .urls import url
6
- from django .core .exceptions import ImproperlyConfigured
6
+ from django .core .exceptions import ImproperlyConfigured , ObjectDoesNotExist
7
7
from django .test import override_settings
8
8
from django .utils .datastructures import MultiValueDict
9
9
@@ -167,6 +167,22 @@ def test_representation_unsaved_object_with_non_nullable_pk(self):
167
167
representation = self .field .to_representation (MockObject (pk = '' ))
168
168
assert representation is None
169
169
170
+ def test_serialize_empty_relationship_attribute (self ):
171
+ class TestSerializer (serializers .Serializer ):
172
+ via_unreachable = serializers .HyperlinkedRelatedField (
173
+ source = 'does_not_exist.unreachable' ,
174
+ view_name = 'example' ,
175
+ read_only = True ,
176
+ )
177
+
178
+ class TestSerializable :
179
+ @property
180
+ def does_not_exist (self ):
181
+ raise ObjectDoesNotExist
182
+
183
+ serializer = TestSerializer (TestSerializable ())
184
+ assert serializer .data == {'via_unreachable' : None }
185
+
170
186
def test_hyperlinked_related_lookup_exists (self ):
171
187
instance = self .field .to_internal_value ('http://example.org/example/foobar/' )
172
188
assert instance is self .queryset .items [0 ]
You can’t perform that action at this time.
0 commit comments