Skip to content

Commit

Permalink
Fix django-json-api#784 Clear m2m relationships instead of deleting t…
Browse files Browse the repository at this point in the history
…hem during PATCH

Calling PATCH on an M2M RelationshipView when there were already some relationships was breaking. This was because the related objects were being deleted instead of just clearing the relationship and starting afresh.
  • Loading branch information
rohithpr committed Jun 10, 2020
1 parent 2adfb5b commit a0dadf5
Showing 1 changed file with 3 additions and 0 deletions.
3 changes: 3 additions & 0 deletions rest_framework_json_api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,11 +281,14 @@ def get(self, request, *args, **kwargs):

def remove_relationships(self, instance_manager, field):
field_object = getattr(instance_manager, field)
class_name = instance_manager.__class__.__name__

if field_object.null:
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 a0dadf5

Please sign in to comment.