Skip to content

Commit

Permalink
Clear m2m relationships instead of deleting related objects during PA…
Browse files Browse the repository at this point in the history
…TCH (#788)

Fixes #784
Fixes #244 

Co-authored-by: Oliver Sauder <os@esite.ch>
  • Loading branch information
rohithpr and sliverc authored Jun 13, 2020
1 parent f87eeaf commit 6e5d995
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Ola Tarkowska <ola@red-aries.com>
Oliver Sauder <os@esite.ch>
Raphael Cohen <raphael.cohen.utt@gmail.com>
Roberto Barreda <roberto.barreda@gmail.com>
Rohith PR <praroh2@gmail.com>
santiavenda <santiavenda2@gmail.com>
Tim Selman <timcbaoth@gmail.com>
Yaniv Peer <yanivpeer@gmail.com>
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ any parts of the framework not mentioned in the documentation should generally b
### Fixed

* Avoid `AttributeError` for PUT and PATCH methods when using `APIView`
* Clear many-to-many relationships instead of deleting related objects during PATCH on `RelationshipView`

### Changed

Expand Down
28 changes: 28 additions & 0 deletions example/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,26 @@ def test_patch_one_to_many_relationship(self):
response = self.client.get(url)
assert response.data == request_data['data']

# retry a second time should end up with same result
response = self.client.patch(url, data=request_data)
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_patch_one_to_many_relaitonship_with_none(self):
url = '/blogs/{}/relationships/entry_set'.format(self.first_entry.id)
request_data = {
'data': None
}
response = self.client.patch(url, data=request_data)
assert response.status_code == 200, response.content.decode()
assert response.data == []

response = self.client.get(url)
assert response.data == []

def test_patch_many_to_many_relationship(self):
url = '/entries/{}/relationships/authors'.format(self.first_entry.id)
request_data = {
Expand All @@ -152,6 +172,14 @@ def test_patch_many_to_many_relationship(self):
response = self.client.get(url)
assert response.data == request_data['data']

# retry a second time should end up with same result
response = self.client.patch(url, data=request_data)
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
2 changes: 2 additions & 0 deletions rest_framework_json_api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,8 @@ def remove_relationships(self, instance_manager, field):
for obj in instance_manager.all():
setattr(obj, field_object.name, None)
obj.save()
elif hasattr(instance_manager, 'clear'):
instance_manager.clear()
else:
instance_manager.all().delete()

Expand Down

0 comments on commit 6e5d995

Please sign in to comment.