Skip to content

Commit 0a07d7b

Browse files
authored
Revert "Fix validation for ListSerializer (#8979)"
This reverts commit e2a4559.
1 parent 09a0c55 commit 0a07d7b

File tree

2 files changed

+1
-74
lines changed

2 files changed

+1
-74
lines changed

rest_framework/serializers.py

+1-13
Original file line numberDiff line numberDiff line change
@@ -603,12 +603,6 @@ def __init__(self, *args, **kwargs):
603603
self.min_length = kwargs.pop('min_length', None)
604604
assert self.child is not None, '`child` is a required argument.'
605605
assert not inspect.isclass(self.child), '`child` has not been instantiated.'
606-
607-
instance = kwargs.get('instance', [])
608-
data = kwargs.get('data', [])
609-
if instance and data:
610-
assert len(data) == len(instance), 'Data and instance should have same length'
611-
612606
super().__init__(*args, **kwargs)
613607
self.child.bind(field_name='', parent=self)
614608

@@ -694,13 +688,7 @@ def to_internal_value(self, data):
694688
ret = []
695689
errors = []
696690

697-
for idx, item in enumerate(data):
698-
if (
699-
hasattr(self, 'instance')
700-
and self.instance
701-
and len(self.instance) > idx
702-
):
703-
self.child.instance = self.instance[idx]
691+
for item in data:
704692
try:
705693
validated = self.run_child_validation(item)
706694
except ValidationError as exc:

tests/test_serializer.py

-61
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import pickle
33
import re
44
import sys
5-
import unittest
65
from collections import ChainMap
76
from collections.abc import Mapping
87

@@ -784,63 +783,3 @@ def test_nested_key(self):
784783
ret = {'a': 1}
785784
self.s.set_value(ret, ['x', 'y'], 2)
786785
assert ret == {'a': 1, 'x': {'y': 2}}
787-
788-
789-
class MyClass(models.Model):
790-
name = models.CharField(max_length=100)
791-
value = models.CharField(max_length=100, blank=True)
792-
793-
app_label = "test"
794-
795-
@property
796-
def is_valid(self):
797-
return self.name == 'valid'
798-
799-
800-
class MyClassSerializer(serializers.ModelSerializer):
801-
class Meta:
802-
model = MyClass
803-
fields = ('id', 'name', 'value')
804-
805-
def validate_value(self, value):
806-
if value and not self.instance.is_valid:
807-
raise serializers.ValidationError(
808-
'Status cannot be set for invalid instance')
809-
return value
810-
811-
812-
class TestMultipleObjectsValidation(unittest.TestCase):
813-
def setUp(self):
814-
self.objs = [
815-
MyClass(name='valid'),
816-
MyClass(name='invalid'),
817-
MyClass(name='other'),
818-
]
819-
820-
def test_multiple_objects_are_validated_separately(self):
821-
822-
serializer = MyClassSerializer(
823-
data=[{'value': 'set', 'id': instance.id} for instance in
824-
self.objs],
825-
instance=self.objs,
826-
many=True,
827-
partial=True,
828-
)
829-
830-
assert not serializer.is_valid()
831-
assert serializer.errors == [
832-
{},
833-
{'value': ['Status cannot be set for invalid instance']},
834-
{'value': ['Status cannot be set for invalid instance']}
835-
]
836-
837-
def test_exception_raised_when_data_and_instance_length_different(self):
838-
839-
with self.assertRaises(AssertionError):
840-
MyClassSerializer(
841-
data=[{'value': 'set', 'id': instance.id} for instance in
842-
self.objs],
843-
instance=self.objs[:-1],
844-
many=True,
845-
partial=True,
846-
)

0 commit comments

Comments
 (0)