@@ -87,20 +87,18 @@ def extract_relationships(fields, resource, resource_instance):
87
87
continue
88
88
89
89
source = field .source
90
- try :
91
- relation_instance_or_manager = getattr (resource_instance , source )
92
- except AttributeError :
93
- # if the field is not defined on the model then we check the serializer
94
- # and if no value is there we skip over the field completely
95
- serializer_method = getattr (field .parent , source , None )
96
- if serializer_method and hasattr (serializer_method , '__call__' ):
97
- relation_instance_or_manager = serializer_method (resource_instance )
98
- else :
99
- continue
100
-
90
+ serializer_method = getattr (field .parent , source , None )
101
91
relation_type = utils .get_related_resource_type (field )
102
92
103
93
if isinstance (field , relations .HyperlinkedIdentityField ):
94
+ try :
95
+ relation_instance_or_manager = getattr (resource_instance , source )
96
+ except AttributeError :
97
+ if serializer_method and hasattr (serializer_method , '__call__' ):
98
+ relation_instance_or_manager = serializer_method (resource_instance )
99
+ else :
100
+ continue
101
+
104
102
# special case for HyperlinkedIdentityField
105
103
relation_data = list ()
106
104
@@ -124,6 +122,14 @@ def extract_relationships(fields, resource, resource_instance):
124
122
continue
125
123
126
124
if isinstance (field , ResourceRelatedField ):
125
+ try :
126
+ relation_instance_or_manager = getattr (resource_instance , source )
127
+ except AttributeError :
128
+ if serializer_method and hasattr (serializer_method , '__call__' ):
129
+ relation_instance_or_manager = serializer_method (resource_instance )
130
+ else :
131
+ continue
132
+
127
133
# special case for ResourceRelatedField
128
134
relation_data = {
129
135
'data' : resource .get (field_name )
@@ -138,8 +144,15 @@ def extract_relationships(fields, resource, resource_instance):
138
144
continue
139
145
140
146
if isinstance (field , (relations .PrimaryKeyRelatedField , relations .HyperlinkedRelatedField )):
141
- relation_id = relation_instance_or_manager .pk if resource .get (field_name ) else None
147
+ try :
148
+ relation = getattr (resource_instance , '%s_id' % field .source )
149
+ except AttributeError :
150
+ if serializer_method and hasattr (serializer_method , '__call__' ):
151
+ relation = serializer_method (resource_instance ).pk
152
+ else :
153
+ continue
142
154
155
+ relation_id = relation if resource .get (field_name ) else None
143
156
relation_data = {
144
157
'data' : (
145
158
OrderedDict ([('type' , relation_type ), ('id' , encoding .force_text (relation_id ))])
@@ -154,6 +167,13 @@ def extract_relationships(fields, resource, resource_instance):
154
167
continue
155
168
156
169
if isinstance (field , relations .ManyRelatedField ):
170
+ try :
171
+ relation_instance_or_manager = getattr (resource_instance , source )
172
+ except AttributeError :
173
+ if serializer_method and hasattr (serializer_method , '__call__' ):
174
+ relation_instance_or_manager = serializer_method (resource_instance )
175
+ else :
176
+ continue
157
177
158
178
if isinstance (field .child_relation , ResourceRelatedField ):
159
179
# special case for ResourceRelatedField
@@ -194,6 +214,14 @@ def extract_relationships(fields, resource, resource_instance):
194
214
continue
195
215
196
216
if isinstance (field , ListSerializer ):
217
+ try :
218
+ relation_instance_or_manager = getattr (resource_instance , source )
219
+ except AttributeError :
220
+ if serializer_method and hasattr (serializer_method , '__call__' ):
221
+ relation_instance_or_manager = serializer_method (resource_instance )
222
+ else :
223
+ continue
224
+
197
225
relation_data = list ()
198
226
199
227
serializer_data = resource .get (field_name )
@@ -211,6 +239,14 @@ def extract_relationships(fields, resource, resource_instance):
211
239
continue
212
240
213
241
if isinstance (field , ModelSerializer ):
242
+ try :
243
+ relation_instance_or_manager = getattr (resource_instance , source )
244
+ except AttributeError :
245
+ if serializer_method and hasattr (serializer_method , '__call__' ):
246
+ relation_instance_or_manager = serializer_method (resource_instance )
247
+ else :
248
+ continue
249
+
214
250
relation_model = field .Meta .model
215
251
relation_type = utils .format_resource_type (relation_model .__name__ )
216
252
0 commit comments