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

Allow child one-to-one instances to be updated without providing PK #29

Closed
wants to merge 2 commits into from
Closed
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
3 changes: 3 additions & 0 deletions drf_writable_nested/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ def update_or_create_reverse_relations(self, instance, reverse_relations):
if related_data is None:
# Skip processing for empty data
continue
pk_name = field.Meta.model._meta.pk.attname
if pk_name not in related_data:
Copy link
Member

@ruscoder ruscoder Nov 19, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The related_data can contain pk attribute instead of pk_name which usually equals to id

related_data[pk_name] = instance.pk
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems the line related_data[pk_name] = instance.pk should be replaced with something like that:

related_instance = getattr(instance, field_source, None)
if related_instance:
    related_data[pk_name] = related_instance.pk

related_data = [related_data]

instances = self.prefetch_related_instances(field, related_data)
Expand Down
106 changes: 106 additions & 0 deletions tests/test_writable_nested_model_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,112 @@ def test_update(self):
# Sites shouldn't be deleted either as it is M2M
self.assertEqual(models.Site.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)
user = serializer.save()

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

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

message_to_update_str_pk = str(user.profile.messages.first().pk)
message_to_update_pk = user.profile.messages.last().pk
serializer = serializers.UserSerializer(
instance=user,
data={
'pk': user_pk,
'username': 'new',
'profile': {
# omit pk
'access_key': None,
'sites': [
{
'url': 'http://new-site.com',
},
],
'avatars': [
{
'pk': user.profile.avatars.earliest('pk').pk,
'image': 'old-image-1.png',
},
{
'image': 'new-image-1.png',
},
{
'image': 'new-image-2.png',
},
],
'messages': [
{
'pk': message_to_update_str_pk,
'message': 'Old message 1'
},
{
'pk': message_to_update_pk,
'message': 'Old message 2'
},
{
'message': 'New message 1'
}
],
},
},
)

serializer.is_valid(raise_exception=True)
user = serializer.save()
user.refresh_from_db()
self.assertIsNotNone(user)
self.assertEqual(user.pk, user_pk)
self.assertEqual(user.username, 'new')

profile = user.profile
self.assertIsNotNone(profile)
self.assertIsNone(profile.access_key)
self.assertEqual(profile.pk, profile_pk)
self.assertEqual(profile.sites.count(), 1)
self.assertSetEqual(
set(profile.sites.values_list('url', flat=True)),
{'http://new-site.com'}
)
self.assertEqual(profile.avatars.count(), 3)
self.assertSetEqual(
set(profile.avatars.values_list('image', flat=True)),
{'old-image-1.png', 'new-image-1.png', 'new-image-2.png'}
)
self.assertSetEqual(
set(profile.messages.values_list('message', flat=True)),
{'Old message 1', 'Old message 2', 'New message 1'}
)
# Check that message which supposed to be updated still in profile
# messages (new message wasn't created instead of update)
self.assertIn(
message_to_update_pk,
profile.messages.values_list('id', flat=True)
)
self.assertIn(
uuid.UUID(message_to_update_str_pk),
profile.messages.values_list('id', flat=True)
)

# Check instances count
self.assertEqual(models.User.objects.count(), 1)
self.assertEqual(models.Profile.objects.count(), 1)
self.assertEqual(models.Avatar.objects.count(), 3)
self.assertEqual(models.Message.objects.count(), 3)
# Access key shouldn't be removed because it is FK
self.assertEqual(models.AccessKey.objects.count(), 1)
# Sites shouldn't be deleted either as it is M2M
self.assertEqual(models.Site.objects.count(), 3)

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