Skip to content

Commit c19a146

Browse files
committed
Fix django-json-api#784 Clear m2m relationships instead of deleting them 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.
1 parent 2adfb5b commit c19a146

File tree

1 file changed

+3
-0
lines changed

1 file changed

+3
-0
lines changed

rest_framework_json_api/views.py

+3
Original file line numberDiff line numberDiff line change
@@ -281,11 +281,14 @@ def get(self, request, *args, **kwargs):
281281

282282
def remove_relationships(self, instance_manager, field):
283283
field_object = getattr(instance_manager, field)
284+
class_name = instance_manager.__class__.__name__
284285

285286
if field_object.null:
286287
for obj in instance_manager.all():
287288
setattr(obj, field_object.name, None)
288289
obj.save()
290+
elif class_name == 'ManyRelatedManager':
291+
instance_manager.clear()
289292
else:
290293
instance_manager.all().delete()
291294

0 commit comments

Comments
 (0)