Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

key name in dict accidentally is the same as method/function name #2244

Closed
Catstyle opened this issue Dec 10, 2014 · 1 comment
Closed

key name in dict accidentally is the same as method/function name #2244

Catstyle opened this issue Dec 10, 2014 · 1 comment
Labels
Milestone

Comments

@Catstyle
Copy link

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?

@tomchristie tomchristie added this to the 3.0.1 Release milestone Dec 10, 2014
@tomchristie
Copy link
Member

Thanks for the reports.
Behavior for dicts should be better now.
Release 3.0.1 incoming shortly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants