source='y.z' just return y if pass dict in Serializer creation or self.get_serializer · Issue #2243 · encode/django-rest-framework (original) (raw)

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