Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes unexpected deletion of related objects. #97

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions drf_writable_nested/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,12 @@ def delete_reverse_relations_if_need(self, instance, reverse_relations):
# Remove relations from m2m table
m2m_manager = getattr(instance, field_source)
m2m_manager.remove(*pks_to_delete)
elif related_field.many_to_one and related_field.null:
# The instance should not be deleted if the object is foreign-key related.
unlink_foreign_key = {
related_field.name: None,
}
model_class.objects.filter(pk__in=pks_to_delete).update(**unlink_foreign_key)
else:
model_class.objects.filter(pk__in=pks_to_delete).delete()

Expand Down
14 changes: 12 additions & 2 deletions tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,25 @@ class CustomPK(models.Model):
)


class Message(models.Model):
class AbstractMessage(models.Model):
id = models.UUIDField(
primary_key=True,
default=uuid.uuid4,
editable=False
)
profile = models.ForeignKey(Profile, on_delete=models.CASCADE)
message = models.CharField(max_length=100)

class Meta:
abstract = True


class Message(AbstractMessage):
profile = models.ForeignKey(Profile, on_delete=models.CASCADE)


class PersistentMessage(AbstractMessage):
profile = models.ForeignKey(Profile, on_delete=models.CASCADE, null=True)


class AnotherProfile(models.Model):
sites = models.ManyToManyField(Site)
Expand Down
21 changes: 21 additions & 0 deletions tests/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ class Meta:
fields = ('pk', 'message',)


class PersistentMessageSerializer(serializers.ModelSerializer):

class Meta:
model = models.PersistentMessage
fields = ('pk', 'message',)


class SiteSerializer(serializers.ModelSerializer):
url = serializers.CharField()

Expand Down Expand Up @@ -54,6 +61,13 @@ class Meta:
fields = ('pk', 'sites', 'avatars', 'access_key', 'message_set',)


class PersistentProfileSerializer(ProfileSerializer):
message_set = PersistentMessageSerializer(many=True)

class Meta(ProfileSerializer.Meta):
pass


class UserSerializer(WritableNestedModelSerializer):
# Reverse OneToOne relation
profile = ProfileSerializer(required=False, allow_null=True)
Expand All @@ -64,6 +78,13 @@ class Meta:
fields = ('pk', 'profile', 'username', 'user_avatar')


class PersistentUserSerializer(WritableNestedModelSerializer):
profile = PersistentProfileSerializer(required=False, allow_null=True)

class Meta(UserSerializer.Meta):
pass


class CustomSerializer(UserSerializer):
# Simulate having non-modelfield information on the serializer
custom_field = serializers.CharField()
Expand Down
43 changes: 43 additions & 0 deletions tests/test_writable_nested_model_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,49 @@ def test_update(self):
# Sites shouldn't be deleted either as it is M2M
self.assertEqual(models.Site.objects.count(), 3)

def test_update_no_delete_if_null_is_false(self):
serializer = serializers.UserSerializer(data=self.get_initial_data())
serializer.is_valid(raise_exception=True)
user = serializer.save()

# Check instances count
self.assertEqual(models.User.objects.count(), 1)
self.assertEqual(models.Message.objects.count(), 3)

# Update
user_pk = user.pk
profile_pk = user.profile.pk

serializer = serializers.PersistentUserSerializer(
data={
'pk': user_pk,
'username': 'new',
'profile': {
'pk': profile_pk,
'access_key': None,
'sites': [
{
'url': 'http://new-site.com',
},
],
'avatars': [
],
'message_set': [
],
},
},
instance=user,
)

serializer.is_valid(raise_exception=True)
user = serializer.save()
user.refresh_from_db()

# Check instances count
self.assertEqual(models.User.objects.count(), 1)
self.assertEqual(models.Avatar.objects.count(), 0)
self.assertEqual(models.Message.objects.count(), 3)

def test_update_reverse_one_to_one_without_pk(self):
serializer = serializers.UserSerializer(data=self.get_initial_data())
serializer.is_valid(raise_exception=True)
Expand Down