Skip to content

Returning an object in Serializer.to_internal_value is not working any more since 3.8.2 #6053

Closed
@slide333333

Description

@slide333333

Returning an object in Serializer.to_internal_value is not working any more since 3.8.2 (because of #5922). I don't know if it was ever intended to be working (since obviously there are no tests for it) but it did in 3.8.1. Now Serializer.to_internal_value always has to return a dict. Otherwise a type error is raised.

Checklist

  • I have verified that that issue exists against the master branch of Django REST framework.
  • I have searched for similar issues in both open and closed tickets and cannot find a duplicate.
  • This is not a usage question. (Those should be directed to the discussion group instead.)
  • This cannot be dealt with as a third party library. (We prefer new functionality to be in the form of third party libraries where possible.)
  • I have reduced the issue to the simplest possible case.
  • I have included a failing test as a pull request. (If you are unable to do so we can still accept the issue.)

Steps to reproduce

class NestedPointSerializer(serializers.Serializer):
    """
    Serialize `django.contrib.gis.geos.point.Point` instances
    """

    longitude = serializers.FloatField(source='x')
    latitude = serializers.FloatField(source='y')

    def to_internal_value(self, data):
        kwargs = super(NestedPointSerializer, self).to_internal_value(data)
        return Point(srid=4326, **kwargs)

NestedPointSerializer(data={'longitude': 6.958307, 'latitude': 50.941357}).is_valid()

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 2910, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-4-fd7c8e4a6eab>", line 1, in <module>
    NestedPointSerializer(data={'longitude': 6.958307, 'latitude': 50.941357}).is_valid()
  File "/usr/local/lib/python3.7/site-packages/rest_framework/serializers.py", line 236, in is_valid
    self._validated_data = self.run_validation(self.initial_data)
  File "/usr/local/lib/python3.7/site-packages/rest_framework/serializers.py", line 436, in run_validation
    self.run_validators(value)
  File "/usr/local/lib/python3.7/site-packages/rest_framework/serializers.py", line 465, in run_validators
    to_validate.update(value)
TypeError: 'float' object is not iterable

Expected behavior

Everything is working like it did in 3.8.1.

Actual behavior

TypeError: 'float' object is not iterable raised in

to_validate.update(value)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions