Skip to content

Commit ddf612d

Browse files
authored
Merge pull request #288 from amw/fix-errors-in-relationship-views
Fix errors in relationship views
2 parents 1941c34 + 0302fab commit ddf612d

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

example/tests/test_views.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,18 @@ def test_patch_invalid_entry_relationship_blog_returns_400(self):
7777
content_type='application/vnd.api+json')
7878
assert response.status_code == 400
7979

80+
def test_relationship_view_errors_format(self):
81+
url = '/entries/{}/relationships/blog'.format(self.first_entry.id)
82+
response = self.client.patch(url,
83+
data=json.dumps({'data': {'invalid': ''}}),
84+
content_type='application/vnd.api+json')
85+
assert response.status_code == 400
86+
87+
result = json.loads(response.content.decode('utf-8'))
88+
89+
assert 'data' not in result
90+
assert 'errors' in result
91+
8092
def test_get_empty_to_one_relationship(self):
8193
url = '/comments/{}/relationships/author'.format(self.first_entry.id)
8294
response = self.client.get(url)

rest_framework_json_api/renderers.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -419,24 +419,24 @@ def render(self, data, accepted_media_type=None, renderer_context=None):
419419
view = renderer_context.get("view", None)
420420
request = renderer_context.get("request", None)
421421

422+
# Get the resource name.
423+
resource_name = utils.get_resource_name(renderer_context)
424+
425+
# If this is an error response, skip the rest.
426+
if resource_name == 'errors':
427+
return self.render_errors(data, accepted_media_type, renderer_context)
428+
422429
from rest_framework_json_api.views import RelationshipView
423430
if isinstance(view, RelationshipView):
424431
return self.render_relationship_view(data, accepted_media_type, renderer_context)
425432

426-
# Get the resource name.
427-
resource_name = utils.get_resource_name(renderer_context)
428-
429433
# If `resource_name` is set to None then render default as the dev
430434
# wants to build the output format manually.
431435
if resource_name is None or resource_name is False:
432436
return super(JSONRenderer, self).render(
433437
data, accepted_media_type, renderer_context
434438
)
435439

436-
# If this is an error response, skip the rest.
437-
if resource_name == 'errors':
438-
return self.render_errors(data, accepted_media_type, renderer_context)
439-
440440
json_api_data = data
441441
json_api_included = list()
442442
# initialize json_api_meta with pagination meta or an empty dict

0 commit comments

Comments
 (0)