Skip to content

Commit 63d0122

Browse files
TayHobbskhornberg
authored andcommitted
Enhancement: Allow relationships to not be included
1 parent 3471569 commit 63d0122

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

example/tests/test_serializers.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,24 @@ def test_model_serializer_with_implicit_fields(self, comment, client):
112112
expected_json = dump_json(expected)
113113

114114
assert actual == expected_json
115+
116+
def test_model_serializer_with_implicit_relationships_respecting_settings(self, comment, client, settings):
117+
settings.JSON_API_IMPLICIT_RELATIONSHIPS = False
118+
expected = {
119+
"data": {
120+
"type": "comments",
121+
"id": str(comment.pk),
122+
"attributes": {
123+
"body": comment.body
124+
}
125+
}
126+
}
127+
128+
response = client.get(reverse("comment-detail", kwargs={'pk': comment.pk}))
129+
130+
assert response.status_code == 200
131+
132+
actual = redump_json(response.content)
133+
expected_json = dump_json(expected)
134+
135+
assert actual == expected_json

rest_framework_json_api/renderers.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import inflection
88
from django.db.models import Manager, QuerySet
99
from django.utils import six, encoding
10+
from django.conf import settings
1011
from rest_framework import relations
1112
from rest_framework import renderers
1213
from rest_framework.serializers import BaseSerializer, Serializer, ListSerializer
@@ -387,9 +388,10 @@ def build_json_resource_obj(fields, resource, resource_instance, resource_name):
387388
('id', encoding.force_text(resource_instance.pk) if resource_instance else None),
388389
('attributes', JSONRenderer.extract_attributes(fields, resource)),
389390
]
390-
relationships = JSONRenderer.extract_relationships(fields, resource, resource_instance)
391-
if relationships:
392-
resource_data.append(('relationships', relationships))
391+
if getattr(settings, 'JSON_API_IMPLICIT_RELATIONSHIPS', True):
392+
relationships = JSONRenderer.extract_relationships(fields, resource, resource_instance)
393+
if relationships:
394+
resource_data.append(('relationships', relationships))
393395
# Add 'self' link if field is present and valid
394396
if api_settings.URL_FIELD_NAME in resource and \
395397
isinstance(fields[api_settings.URL_FIELD_NAME], relations.RelatedField):

0 commit comments

Comments
 (0)