Skip to content

SerializerMethodResourceRelatedField with many=True does not show up in included #260

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
Aug 18, 2016
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 example/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def __init__(self, *args, **kwargs):
'authors': 'example.serializers.AuthorSerializer',
'comments': 'example.serializers.CommentSerializer',
'featured': 'example.serializers.EntrySerializer',
'suggested': 'example.serializers.EntrySerializer',
}

body_format = serializers.SerializerMethodField()
Expand Down
9 changes: 9 additions & 0 deletions example/tests/integration/test_includes.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ def test_dynamic_related_data_is_included(single_entry, entry_factory, client):
assert len(included) == 1, 'The dynamically included blog entries are of an incorrect count'


def test_dynamic_many_related_data_is_included(single_entry, entry_factory, client):
entry_factory()
response = client.get(reverse("entry-detail", kwargs={'pk': single_entry.pk}) + '?include=suggested')
included = load_json(response.content).get('included')

assert included
assert [x.get('type') for x in included] == ['entries'], 'Dynamic included types are incorrect'


def test_missing_field_not_included(author_bio_factory, author_factory, client):
# First author does not have a bio
author = author_factory(bio=None)
Expand Down
7 changes: 6 additions & 1 deletion rest_framework_json_api/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,12 @@ def extract_included(fields, resource, resource_instance, included_resources):
serializer_class = included_serializers.get(field_name)
if relation_instance_or_manager is None:
continue
field = serializer_class(relation_instance_or_manager, context=context)

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

field = serializer_class(relation_instance_or_manager,
many=many,
context=context)
serializer_data = field.data

if isinstance(field, ListSerializer):
Expand Down