mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-04 12:30:11 +03:00
Fix #1643 issue: empty values in reverse nested OneToMany are not validated by the serializer
This commit is contained in:
parent
e11f41ebc4
commit
0779dcb748
|
@ -446,7 +446,7 @@ class BaseSerializer(WritableField):
|
||||||
if not self._errors:
|
if not self._errors:
|
||||||
into.update(reverted_data)
|
into.update(reverted_data)
|
||||||
else:
|
else:
|
||||||
if value in (None, ''):
|
if value in (None, '') and not self.many:
|
||||||
into[(self.source or field_name)] = None
|
into[(self.source or field_name)] = None
|
||||||
else:
|
else:
|
||||||
# Set the serializer object if it exists
|
# Set the serializer object if it exists
|
||||||
|
|
|
@ -283,6 +283,13 @@ class ReverseNestedOneToManyTests(TestCase):
|
||||||
self.assertFalse(serializer.is_valid())
|
self.assertFalse(serializer.is_valid())
|
||||||
self.assertEqual(serializer.errors, {'sources': [{}, {}, {}, {'name': ['This field is required.']}]})
|
self.assertEqual(serializer.errors, {'sources': [{}, {}, {}, {'name': ['This field is required.']}]})
|
||||||
|
|
||||||
|
def test_one_to_many_create_with_empty_string(self):
|
||||||
|
data = {'id': 1, 'name': 'target-1', 'sources': ''}
|
||||||
|
instance = OneToManyTarget.objects.get(pk=1)
|
||||||
|
serializer = self.Serializer(instance, data=data)
|
||||||
|
self.assertFalse(serializer.is_valid())
|
||||||
|
self.assertEqual(serializer.errors, {'sources': [{u'non_field_errors': [u'Expected a list of items.']}]})
|
||||||
|
|
||||||
def test_one_to_many_update(self):
|
def test_one_to_many_update(self):
|
||||||
data = {'id': 1, 'name': 'target-1-updated', 'sources': [{'id': 1, 'name': 'source-1-updated'},
|
data = {'id': 1, 'name': 'target-1-updated', 'sources': [{'id': 1, 'name': 'source-1-updated'},
|
||||||
{'id': 2, 'name': 'source-2'},
|
{'id': 2, 'name': 'source-2'},
|
||||||
|
|
Loading…
Reference in New Issue
Block a user