@@ -87,20 +87,18 @@ def extract_relationships(fields, resource, resource_instance):
8787 continue
8888
8989 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 )
10191 relation_type = utils .get_related_resource_type (field )
10292
10393 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+
104102 # special case for HyperlinkedIdentityField
105103 relation_data = list ()
106104
@@ -124,6 +122,14 @@ def extract_relationships(fields, resource, resource_instance):
124122 continue
125123
126124 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+
127133 # special case for ResourceRelatedField
128134 relation_data = {
129135 'data' : resource .get (field_name )
@@ -138,8 +144,15 @@ def extract_relationships(fields, resource, resource_instance):
138144 continue
139145
140146 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
142154
155+ relation_id = relation if resource .get (field_name ) else None
143156 relation_data = {
144157 'data' : (
145158 OrderedDict ([('type' , relation_type ), ('id' , encoding .force_text (relation_id ))])
@@ -154,6 +167,13 @@ def extract_relationships(fields, resource, resource_instance):
154167 continue
155168
156169 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
157177
158178 if isinstance (field .child_relation , ResourceRelatedField ):
159179 # special case for ResourceRelatedField
@@ -194,6 +214,14 @@ def extract_relationships(fields, resource, resource_instance):
194214 continue
195215
196216 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+
197225 relation_data = list ()
198226
199227 serializer_data = resource .get (field_name )
@@ -211,6 +239,14 @@ def extract_relationships(fields, resource, resource_instance):
211239 continue
212240
213241 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+
214250 relation_model = field .Meta .model
215251 relation_type = utils .format_resource_type (relation_model .__name__ )
216252
0 commit comments