Closed
Description
this is the situation 1 mentioned in #2237
In [1]: from rest_framework import serializers
In [2]: class Foo(serializers.Serializer):
items = serializers.ListSerializer(child=serializers.IntegerField())
...:
In [3]: s = Foo({'items': [1, 2, 3]})
In [4]: s.data
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-4-b0eddb6f5f12> in <module>()
----> 1 s.data
.../rest_framework/serializers.pyc in data(self)
420 @property
421 def data(self):
--> 422 ret = super(Serializer, self).data
423 return ReturnDict(ret, serializer=self)
424
.../rest_framework/serializers.pyc in data(self)
175 if not hasattr(self, '_data'):
176 if self.instance is not None and not getattr(self, '_errors', None):
--> 177 self._data = self.to_representation(self.instance)
178 elif hasattr(self, '_validated_data') and not getattr(self, '_errors', None):
179 self._data = self.to_representation(self.validated_data)
.../rest_framework/serializers.pyc in to_representation(self, instance)
389 ret[field.field_name] = None
390 else:
--> 391 ret[field.field_name] = field.to_representation(attribute)
392
393 return ret
.../rest_framework/serializers.pyc in to_representation(self, data)
520 iterable = data.all() if (hasattr(data, 'all')) else data
521 return [
--> 522 self.child.to_representation(item) for item in iterable
523 ]
524
TypeError: 'builtin_function_or_method' object is not iterable
fields.py
62 try:
63 instance = getattr(instance, attr)
64 except ObjectDoesNotExist:
65 return None
66 except AttributeError as exc:
67 try:
68 return instance[attr]
69 except (KeyError, TypeError, AttributeError):
70 raise exc
it just return dict.items instead of dict['items'], should it check instance[attr] first?