From c13f132a21f5a0902cef74dfea12e586561774ca Mon Sep 17 00:00:00 2001 From: Yuri Prezument Date: Tue, 18 Dec 2012 14:09:58 +0200 Subject: [PATCH 1/3] Failing test with partial serializer and foreign keys --- rest_framework/tests/serializer.py | 32 ++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/rest_framework/tests/serializer.py b/rest_framework/tests/serializer.py index 780177aa0..693c81c29 100644 --- a/rest_framework/tests/serializer.py +++ b/rest_framework/tests/serializer.py @@ -297,6 +297,38 @@ class ModelValidationTests(TestCase): self.assertFalse(second_serializer.is_valid()) self.assertEqual(second_serializer.errors, {'title': [u'Album with this Title already exists.']}) + def test_foreign_key_with_partial(self): + """ + Test ModelSerializer validation with partial=True + + Specifically test foreign key validation. + """ + + album = Album(title='test') + album.save() + + class PhotoSerializer(serializers.ModelSerializer): + class Meta: + model = Photo + + photo_serializer = PhotoSerializer(data={'description': 'test', 'album': album.pk}) + self.assertTrue(photo_serializer.is_valid()) + photo = photo_serializer.save() + + # Updating only the album (foreign key) + photo_serializer = PhotoSerializer(instance=photo, data={'album': album.pk}, partial=True) + self.assertTrue(photo_serializer.is_valid()) + self.assertTrue(photo_serializer.save()) + + # Updating only the description + photo_serializer = PhotoSerializer(instance=photo, + data={'description': 'new'}, + partial=True) + + self.assertTrue(photo_serializer.is_valid()) + self.assertTrue(photo_serializer.save()) + + class RegexValidationTest(TestCase): def test_create_failed(self): From 5c680c36e435ffa29ba6ed5f439d22ea4e849c67 Mon Sep 17 00:00:00 2001 From: Mark Aaron Shirley Date: Wed, 19 Dec 2012 09:26:40 -0800 Subject: [PATCH 2/3] Check if RelatedField is required. --- rest_framework/fields.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/rest_framework/fields.py b/rest_framework/fields.py index 2060e3e70..b0dfdce8c 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -351,6 +351,11 @@ class RelatedField(WritableField): if self.read_only: return + if field_name not in data and self.required: + raise ValidationError(self.error_messages['required']) + elif field_name not in data: + return + value = data.get(field_name) if value in (None, '') and not self.null: From f8a1256b1c006ee9ab46645a11ef19123b429a56 Mon Sep 17 00:00:00 2001 From: Mark Aaron Shirley Date: Fri, 21 Dec 2012 11:33:01 -0800 Subject: [PATCH 3/3] Update RelatedField#field_from_native coding style --- rest_framework/fields.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/rest_framework/fields.py b/rest_framework/fields.py index 1059df918..dd90c3f86 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -351,13 +351,13 @@ class RelatedField(WritableField): if self.read_only: return - if field_name not in data and self.required: - raise ValidationError(self.error_messages['required']) - elif field_name not in data: + try: + value = data[field_name] + except KeyError: + if self.required: + raise ValidationError(self.error_messages['required']) return - value = data.get(field_name) - if value in (None, '') and not self.null: raise ValidationError('Value may not be null') elif value in (None, '') and self.null: