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

source='y.z' just return y if pass dict in Serializer creation or self.get_serializer #2243

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

Comments

@Catstyle
Copy link

yeah, me again, still error in get_attribute, this time i'd like to create a new issue because i think it may be a bug

In [3]: class Foo(serializers.Serializer):

    x = serializers.IntegerField(source='y.z')
   ...:     

In [4]: s = Foo({'y': {'z': 1}})

In [5]: s.data
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-5-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/fields.pyc in to_representation(self, value)

TypeError: int() argument must be a string or a number, not 'dict'

In [6]: s = Foo(data={'y': {'z': 1}})

In [7]: s.data
Out[7]: ReturnDict()
fields.py
  66         except AttributeError as exc:                                           
  67             try:                                                                
  68                 return instance[attr]                                           
  69             except (KeyError, TypeError, AttributeError):                       
  70                 raise exc 

simply i think it should be

  66         except AttributeError as exc:                                           
  67             try:                                                                
  68                 instance = instance[attr]                                           
  69             except (KeyError, TypeError, AttributeError):                       
  70                 raise exc 
@tomchristie
Copy link
Member

Yup, agreed.

@tomchristie tomchristie added this to the 3.0.1 Release milestone Dec 10, 2014
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