From 36cf32b545fe48f8161edecf02cb5a50749230ff Mon Sep 17 00:00:00 2001 From: Leifur Halldor Asgeirsson Date: Tue, 22 Sep 2015 13:25:31 -0400 Subject: [PATCH 1/2] Fix crash with serializers that have fields which do not exist on the model. Change utils.extract_relationships to skip fields defined on the serializer that don't correspond to a field on the model. --- rest_framework_json_api/utils.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/rest_framework_json_api/utils.py b/rest_framework_json_api/utils.py index e37f4110..e41ef0f0 100644 --- a/rest_framework_json_api/utils.py +++ b/rest_framework_json_api/utils.py @@ -229,7 +229,10 @@ def extract_relationships(fields, resource, resource_instance): continue relation_type = get_related_resource_type(field) - relation_instance_or_manager = getattr(resource_instance, field_name) + try: + relation_instance_or_manager = getattr(resource_instance, field_name) + except AttributeError: # Skip fields defined on the serializer that don't correspond to a field on the model + continue if isinstance(field, HyperlinkedIdentityField): # special case for HyperlinkedIdentityField From 4e95bb41a8bd1e4ac32bf9754b1757ebf78eb336 Mon Sep 17 00:00:00 2001 From: Leifur Halldor Asgeirsson Date: Tue, 22 Sep 2015 13:47:05 -0400 Subject: [PATCH 2/2] get related instance or manager before getting relation type in case this is a custom field that doesn't exist on the model --- rest_framework_json_api/utils.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/rest_framework_json_api/utils.py b/rest_framework_json_api/utils.py index e41ef0f0..24adda77 100644 --- a/rest_framework_json_api/utils.py +++ b/rest_framework_json_api/utils.py @@ -228,12 +228,13 @@ def extract_relationships(fields, resource, resource_instance): if not isinstance(field, (RelatedField, ManyRelatedField, BaseSerializer)): continue - relation_type = get_related_resource_type(field) try: relation_instance_or_manager = getattr(resource_instance, field_name) except AttributeError: # Skip fields defined on the serializer that don't correspond to a field on the model continue + relation_type = get_related_resource_type(field) + if isinstance(field, HyperlinkedIdentityField): # special case for HyperlinkedIdentityField relation_data = list()