Skip to content

Avoid exception when trying to include skipped relationship #453

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 5 commits into from
Feb 11, 2019
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ Yaniv Peer <[email protected]>
Mohammed Ali Zubair <[email protected]>
Jason Housley <[email protected]>
Beni Keller <[email protected]>
Stas S. <[email protected]>
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
Note that in line with [Django REST Framework policy](http://www.django-rest-framework.org/topics/release-notes/),
any parts of the framework not mentioned in the documentation should generally be considered private API, and may be subject to change.

## [Unreleased]

### Fixed

* Avoid exception when trying to include skipped relationship

## [2.7.0] - 2019-01-14

Expand Down
27 changes: 26 additions & 1 deletion example/serializers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from datetime import datetime

from rest_framework import serializers as drf_serilazers
from rest_framework import serializers as drf_serilazers, fields as drf_fields

from rest_framework_json_api import relations, serializers

Expand Down Expand Up @@ -318,15 +318,40 @@ class Meta:
exclude = ('polymorphic_ctype',)


class CurrentProjectRelatedField(relations.PolymorphicResourceRelatedField):
def get_attribute(self, instance):
obj = super(CurrentProjectRelatedField, self).get_attribute(instance)

is_art = (
self.field_name == 'current_art_project' and
isinstance(obj, ArtProject)
)
is_res = (
self.field_name == 'current_research_project' and
isinstance(obj, ResearchProject)
)

if is_art or is_res:
return obj

raise drf_fields.SkipField()


class CompanySerializer(serializers.ModelSerializer):
current_project = relations.PolymorphicResourceRelatedField(
ProjectSerializer, queryset=Project.objects.all())
current_art_project = CurrentProjectRelatedField(
ProjectSerializer, source='current_project', read_only=True)
current_research_project = CurrentProjectRelatedField(
ProjectSerializer, source='current_project', read_only=True)
future_projects = relations.PolymorphicResourceRelatedField(
ProjectSerializer, queryset=Project.objects.all(), many=True)

included_serializers = {
'current_project': ProjectSerializer,
'future_projects': ProjectSerializer,
'current_art_project': ProjectSerializer,
'current_research_project': ProjectSerializer
}

class Meta:
Expand Down
7 changes: 5 additions & 2 deletions example/tests/integration/test_polymorphism.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,13 @@ def test_polymorphism_on_detail_relations(single_company, client):


def test_polymorphism_on_included_relations(single_company, client):
response = client.get(reverse("company-detail", kwargs={'pk': single_company.pk}) +
'?include=current_project,future_projects')
response = client.get(
reverse("company-detail", kwargs={'pk': single_company.pk}) +
'?include=current_project,future_projects,current_art_project,current_research_project')
content = response.json()
assert content["data"]["relationships"]["currentProject"]["data"]["type"] == "artProjects"
assert content["data"]["relationships"]["currentArtProject"]["data"]["type"] == "artProjects"
assert content["data"]["relationships"]["currentResearchProject"]["data"] is None
assert (
set([rel["type"] for rel in content["data"]["relationships"]["futureProjects"]["data"]]) ==
set(["researchProjects", "artProjects"])
Expand Down
2 changes: 1 addition & 1 deletion rest_framework_json_api/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ def extract_included(cls, fields, resource, resource_instance, included_resource
serializer_data = field.data

if isinstance(field, relations.RelatedField):
if relation_instance is None:
if relation_instance is None or not serializer_data:
continue

many = field._kwargs.get('child_relation', None) is not None
Expand Down