diff --git a/example/tests/test_views.py b/example/tests/test_views.py index 28c39915..d802ef52 100644 --- a/example/tests/test_views.py +++ b/example/tests/test_views.py @@ -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)}, ] @@ -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 = { diff --git a/rest_framework_json_api/views.py b/rest_framework_json_api/views.py index 77bf9b91..ee368851 100644 --- a/rest_framework_json_api/views.py +++ b/rest_framework_json_api/views.py @@ -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: