Skip to content

Commit ea6cbfc

Browse files
author
Oliver Sauder
committed
Add changelog entry for patch RelationshipView patch
1 parent dfaa30d commit ea6cbfc

File tree

3 files changed

+19
-11
lines changed

3 files changed

+19
-11
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ any parts of the framework not mentioned in the documentation should generally b
2323
### Fixed
2424

2525
* Pass context from `PolymorphicModelSerializer` to child serializers to support fields which require a `request` context such as `url`.
26+
* Avoid patch on `RelationshipView` deleting relationship instance when constraint would allow null ([#242](https://github.com/django-json-api/django-rest-framework-json-api/issues/242))
2627

2728

2829
## [2.6.0] - 2018-09-20

example/tests/test_views.py

+13-5
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
from . import TestBase
1313
from .. import views
14-
from example.factories import AuthorFactory, EntryFactory, CommentFactory
14+
from example.factories import AuthorFactory, CommentFactory, EntryFactory
1515
from example.models import Author, Blog, Comment, Entry
1616
from example.serializers import AuthorBioSerializer, AuthorTypeSerializer, EntrySerializer
1717
from example.views import AuthorViewSet
@@ -240,10 +240,14 @@ def test_new_comment_data_patch_to_many_relationship(self):
240240
previous_response = {
241241
'data': [
242242
{'type': 'comments',
243-
'id': f'{self.second_comment.id}'
243+
'id': str(self.second_comment.id)
244244
}
245245
],
246-
'links': {'self': f'http://testserver/authors/{self.author.id}/relationships/comment_set'}
246+
'links': {
247+
'self': 'http://testserver/authors/{}/relationships/comment_set'.format(
248+
self.author.id
249+
)
250+
}
247251
}
248252

249253
response = self.client.get(url)
@@ -256,14 +260,18 @@ def test_new_comment_data_patch_to_many_relationship(self):
256260
'id': f'{comment.id}'
257261
}
258262
],
259-
'links': {'self': f'http://testserver/authors/{self.author.id}/relationships/comment_set'}
263+
'links': {
264+
'self': 'http://testserver/authors/{}/relationships/comment_set'.format(
265+
self.author.id
266+
)
267+
}
260268
}
261269

262270
response = self.client.patch(url, data=request_data)
263271
assert response.status_code == 200
264272
assert response.json() == new_patched_response
265273

266-
assert Comment.objects.filter(id=self.second_comment.id).exists
274+
assert Comment.objects.filter(id=self.second_comment.id).exists()
267275

268276

269277
class TestRelatedMixin(APITestCase):

rest_framework_json_api/views.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ def get(self, request, *args, **kwargs):
254254
def remove_relationships(self, instance_manager, field):
255255
field_object = getattr(instance_manager, field)
256256

257-
if getattr(field_object, "null"):
257+
if field_object.null:
258258
for obj in instance_manager.all():
259259
setattr(obj, field_object.name, None)
260260
obj.save()
@@ -273,16 +273,15 @@ def patch(self, request, *args, **kwargs):
273273
data=request.data, model_class=related_model_class, many=True
274274
)
275275
serializer.is_valid(raise_exception=True)
276-
# related_instance_or_manager.all().delete()
277276

278277
# for to one
279278
if hasattr(related_instance_or_manager, "field"):
280-
related_instance_or_manager = self.remove_relationships(instance_manager=related_instance_or_manager,
281-
field="field")
279+
related_instance_or_manager = self.remove_relationships(
280+
instance_manager=related_instance_or_manager, field="field")
282281
# for to many
283282
else:
284-
related_instance_or_manager = self.remove_relationships(instance_manager=related_instance_or_manager,
285-
field="target_field")
283+
related_instance_or_manager = self.remove_relationships(
284+
instance_manager=related_instance_or_manager, field="target_field")
286285

287286
# have to set bulk to False since data isn't saved yet
288287
class_name = related_instance_or_manager.__class__.__name__

0 commit comments

Comments
 (0)