Skip to content
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
21 changes: 20 additions & 1 deletion example/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def test_patch_to_one_relationship(self):
response = self.client.get(url)
assert response.data == request_data['data']

def test_patch_to_many_relationship(self):
def test_patch_one_to_many_relationship(self):
url = '/blogs/{}/relationships/entry_set'.format(self.first_entry.id)
request_data = {
'data': [{'type': format_resource_type('Entry'), 'id': str(self.first_entry.id)}, ]
Expand All @@ -130,6 +130,25 @@ def test_patch_to_many_relationship(self):
response = self.client.get(url)
assert response.data == request_data['data']

def test_patch_many_to_many_relationship(self):
url = '/entries/{}/relationships/authors'.format(self.first_entry.id)
request_data = {
'data': [
{
'type': format_resource_type('Author'),
'id': str(self.author.id)
},
]
}
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']

def test_post_to_one_relationship_should_fail(self):
url = '/entries/{}/relationships/blog'.format(self.first_entry.id)
request_data = {
Expand Down
3 changes: 2 additions & 1 deletion rest_framework_json_api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ def patch(self, request, *args, **kwargs):
serializer.is_valid(raise_exception=True)
related_instance_or_manager.all().delete()
# have to set bulk to False since data isn't saved yet
if django.VERSION >= (1, 9):
class_name = related_instance_or_manager.__class__.__name__
if django.VERSION >= (1, 9) and class_name != 'ManyRelatedManager':
related_instance_or_manager.add(*serializer.validated_data,
bulk=False)
else:
Expand Down