Skip to content

Extract type for each related instance #94

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
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
23 changes: 11 additions & 12 deletions rest_framework_json_api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,9 @@ def build_json_resource_obj(fields, resource, resource_instance, resource_name):


def get_related_resource_type(relation):
if hasattr(relation, 'get_queryset') and relation.get_queryset() is not None:
if hasattr(relation, '_meta'):
relation_model = relation._meta.model
elif hasattr(relation, 'get_queryset') and relation.get_queryset() is not None:
relation_model = relation.get_queryset().model
else:
parent_serializer = relation.parent
Expand Down Expand Up @@ -266,11 +268,10 @@ def extract_relationships(fields, resource, resource_instance):

if isinstance(field, ManyRelatedField):
relation_data = list()
related_object = field.child_relation
relation_type = get_related_resource_type(related_object)
for related_object in relation_instance_or_manager.all():
related_object_type = get_related_resource_type(related_object)
relation_data.append(OrderedDict([
('type', relation_type),
('type', related_object_type),
('id', encoding.force_text(related_object.pk))
]))
data.update({
Expand All @@ -285,20 +286,18 @@ def extract_relationships(fields, resource, resource_instance):

if isinstance(field, ListSerializer):
relation_data = list()
serializer = field.child
relation_model = serializer.Meta.model
relation_type = format_relation_name(relation_model.__name__)

serializer_data = resource.get(field_name)
resource_instance_queryset = relation_instance_or_manager.all()
if isinstance(serializer_data, list):
for position in range(len(serializer_data)):
nested_resource_instance = resource_instance_queryset[position]
relation_data.append(
OrderedDict(
[('type', relation_type), ('id', encoding.force_text(nested_resource_instance.pk))]
)
)
nested_resource_instance_type = get_related_resource_type(
nested_resource_instance)
relation_data.append(OrderedDict([
('type', nested_resource_instance_type),
('id', encoding.force_text(nested_resource_instance.pk))
]))

data.update({field_name: {'data': relation_data}})
continue
Expand Down