diff options
| author | Chuck Harmston | 2013-12-06 14:00:23 -0600 |
|---|---|---|
| committer | Chuck Harmston | 2013-12-06 14:00:23 -0600 |
| commit | cf6c11bd4b7e7fdaa1de659d69792030e565412a (patch) | |
| tree | aefd1b7a9f75e7a50c1fac28f6b9a1889eb99553 /rest_framework/tests/test_serializer.py | |
| parent | e155534f4f11a3d17b64e7b2e4aad6f002eb271c (diff) | |
| download | django-rest-framework-cf6c11bd4b7e7fdaa1de659d69792030e565412a.tar.bz2 | |
Raise appropriate error in serializer when making a partial update to set a required RelatedField to null (issue #1158)
Diffstat (limited to 'rest_framework/tests/test_serializer.py')
| -rw-r--r-- | rest_framework/tests/test_serializer.py | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/rest_framework/tests/test_serializer.py b/rest_framework/tests/test_serializer.py index 1f85a474..eca467ee 100644 --- a/rest_framework/tests/test_serializer.py +++ b/rest_framework/tests/test_serializer.py @@ -558,6 +558,29 @@ class ModelValidationTests(TestCase): self.assertFalse(second_serializer.is_valid()) self.assertEqual(second_serializer.errors, {'title': ['Album with this Title already exists.']}) + def test_foreign_key_is_null_with_partial(self): + """ + Test ModelSerializer validation with partial=True + + Specifically test that a null foreign key does not pass 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': ''}, partial=True) + self.assertFalse(photo_serializer.is_valid()) + self.assertTrue('album' in photo_serializer.errors) + self.assertEqual(photo_serializer.errors['album'], photo_serializer.error_messages['required']) + def test_foreign_key_with_partial(self): """ Test ModelSerializer validation with partial=True |
