diff --git a/jsonapi_requests/orm/fields.py b/jsonapi_requests/orm/fields.py index a94b69b..ff777b4 100644 --- a/jsonapi_requests/orm/fields.py +++ b/jsonapi_requests/orm/fields.py @@ -124,7 +124,14 @@ def get_object_key(self): except KeyError: raise ObjectKeyError else: - data = relationship.data + data = None + if (hasattr(relationship, 'data') and relationship.data.as_data() != {}) or relationship.as_data() == {}: + data = relationship.data + elif hasattr(relationship, 'links') and relationship.links != {}: + res = self.instance._options.api.endpoint(path=relationship.links.as_data()['self']) + relationship_data = res.get() + data = relationship_data.data + if data.type is None or data.id is None: raise ObjectKeyError else: diff --git a/jsonapi_requests/request_factory.py b/jsonapi_requests/request_factory.py index 0af3d89..23c3004 100644 --- a/jsonapi_requests/request_factory.py +++ b/jsonapi_requests/request_factory.py @@ -36,8 +36,8 @@ def request(self, api_path, method, *, object: data.JsonApiObject=None, **kwargs @property def retrying(self): retry_condition = ( - tenacity.retry_if_exception_type(ApiConnectionError) - | tenacity.retry_if_exception_type(ApiInternalServerError) + tenacity.retry_if_exception_type(ApiConnectionError) | + tenacity.retry_if_exception_type(ApiInternalServerError) ) return tenacity.Retrying( reraise=True, diff --git a/tests/test_orm.py b/tests/test_orm.py index f7e40ab..838847e 100644 --- a/tests/test_orm.py +++ b/tests/test_orm.py @@ -106,6 +106,30 @@ class Meta: test = Test.from_response_content(response_content) assert test.other is None + def test_from_response_with_relationship_with_links_only(self): + mock_api = mock.MagicMock() + mock_api.endpoint().get().data = data.ResourceIdentifier(type='test', id='159') + response_content = data.JsonApiResponse( + data=data.JsonApiObject(id='1', type='test', relationships={ + 'other': data.Relationship(links=data.Dictionary(self='http://example.org/api/rest/test/1/relationships' + '/vendor', + related='http://example.org/api/rest/test/1/vendor' + ) + ) + }) + ) + orm_api = orm.OrmApi(mock_api) + + class Test(orm.ApiModel): + class Meta: + api = orm_api + type = 'test' + other = orm.RelationField(source='other') + name = orm.AttributeField(source='name') + test = Test.from_response_content(response_content) + assert test.other.type is 'test' + assert test.other.id is '159' + def test_issue_19_attributes_are_readable_with_multiple_relations(self): response_content = data.JsonApiResponse( data=data.JsonApiObject(