List serializer bug with only one object passed · Issue #4606 · encode/django-rest-framework (original) (raw)
There is a bug when validating only one object with a list serializer with raise_exception flag set as True.
It worked just fine in 3.4.7.
Steps to reproduce
Here is the class that will cause the issue
class TheSerializer(serializers.Serializer):
name = serializers.CharField(required=True)
choices = serializers.CharField(required=True)
Then run
serializer = TheSerializer(data=[{"name": "hhj"}], many=True)
serializer.is_valid(raise_exception=True)
Expected behavior
if self._errors and raise_exception:
> raise ValidationError(self.errors)
E rest_framework.exceptions.ValidationError: [{'choices': ['This field is required.']}]
Actual behavior
self = TheSerializer(data=[{'name': 'hhj'}], many=True):
name = CharField(required=True)
choices = CharField(required=True)
@property
def errors(self):
ret = super(ListSerializer, self).errors
> if isinstance(ret, list) and len(ret) == 1 and ret[0].code == 'null':
E AttributeError: 'dict' object has no attribute 'code'