Skip to content

HyperlinkedIdentityField support #80

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 14, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions rest_framework_json_api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
from django.utils import six, encoding
from django.utils.translation import ugettext_lazy as _
from rest_framework.serializers import BaseSerializer, ListSerializer, ModelSerializer
from rest_framework.relations import RelatedField, HyperlinkedRelatedField, PrimaryKeyRelatedField
from rest_framework.relations import RelatedField, HyperlinkedRelatedField, PrimaryKeyRelatedField, \
HyperlinkedIdentityField
from rest_framework.settings import api_settings
from rest_framework.exceptions import APIException

Expand Down Expand Up @@ -152,10 +153,15 @@ def get_related_resource_type(relation):
parent_model = parent_serializer.Meta.model
else:
parent_model = parent_serializer.parent.Meta.model
parent_model_relation = getattr(
parent_model,
(relation.source if relation.source else parent_serializer.field_name)
)

if relation.source:
if relation.source != '*':
parent_model_relation = getattr(parent_model, relation.source)
else:
parent_model_relation = getattr(parent_model, relation.field_name)
else:
parent_model_relation = getattr(parent_model, parent_serializer.field_name)

if hasattr(parent_model_relation, 'related'):
relation_model = parent_model_relation.related.model
elif hasattr(parent_model_relation, 'field'):
Expand Down Expand Up @@ -213,8 +219,8 @@ def extract_relationships(fields, resource, resource_instance):
if not isinstance(field, (RelatedField, ManyRelatedField, BaseSerializer)):
continue

if isinstance(field, HyperlinkedRouterField):
# special case for HyperlinkedRouterField
if isinstance(field, HyperlinkedIdentityField):
# special case for HyperlinkedIdentityField
relation_data = list()
relation_type = get_related_resource_type(field)
related = getattr(resource_instance, field_name).all()
Expand Down