Skip to content

Fix return of stale data on PATCH of to-one relationship #247

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
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions example/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ def test_patch_to_one_relationship(self):
}
response = self.client.patch(url, data=json.dumps(request_data), content_type='application/vnd.api+json')
assert response.status_code == 200, response.content.decode()
assert response.data == request_data['data']
Copy link
Contributor

@scottfisk scottfisk May 31, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tpilara Does these 2 test lines cause the tests to fail without the fix?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@scottfisk The line fails on test_patch_to_one_relationship without the fix. It passes before and after on test_patch_to_many_relationship, but I added it just to make sure that a future change doesn't cause it to stop working since only checking a status code of 200 doesn't make any guarantees about the integrity of the data in the response.


response = self.client.get(url)
assert response.data == request_data['data']
Expand All @@ -112,6 +113,7 @@ def test_patch_to_many_relationship(self):
}
response = self.client.patch(url, data=json.dumps(request_data), content_type='application/vnd.api+json')
assert response.status_code == 200, response.content.decode()
assert response.data == request_data['data']

response = self.client.get(url)
assert response.data == request_data['data']
Expand Down
1 change: 1 addition & 0 deletions rest_framework_json_api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ def patch(self, request, *args, **kwargs):
serializer.is_valid(raise_exception=True)
setattr(parent_obj, self.get_related_field_name(), serializer.validated_data)
parent_obj.save()
related_instance_or_manager = self.get_related_instance() # Refresh instance
result_serializer = self._instantiate_serializer(related_instance_or_manager)
return Response(result_serializer.data)

Expand Down