Skip to content

model arrayfield with float child and null True can not be serialized #3685

@mikofski

Description

@mikofski

DRF: 3.2.5
Django: 1.8
PostgreSQL: 9.4
Python: 2.7.10
os: Windows 7-x64

expected:
a model with a PostgreSQL ArrayField, base_Field models.FloatField with null=True should serialize when any item in the array field is None

observed:
the ListField child to_representation raises an exception

float() argument must be a string or a number

Basically what I think is happening is that the serializer.to_representation normally skips the field.to_representation call if the attributes value is None but it doesn't seem to have any way to handle the ListField children's attributes, which might be None. Maybe this is expected behavior? Or it is updated in DRF-3.3? Unfortunately I had trouble with base python classes, not django models in drf-3.3.

full django traceback

Environment:


Request Method: GET
Request URL: http://localhost:8000/requests/

Django Version: 1.8.5
Python Version: 2.7.10
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'simengapi_app',
 'rest_framework')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'django.middleware.security.SecurityMiddleware')


Traceback:
File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
  132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Python27\lib\site-packages\django\views\decorators\csrf.py" in wrapped_view
  58.         return view_func(*args, **kwargs)
File "C:\Python27\lib\site-packages\rest_framework\viewsets.py" in view
  87.             return self.dispatch(request, *args, **kwargs)
File "C:\Python27\lib\site-packages\rest_framework\views.py" in dispatch
  466.             response = self.handle_exception(exc)
File "C:\Python27\lib\site-packages\rest_framework\views.py" in dispatch
  463.             response = handler(request, *args, **kwargs)
File "C:\Python27\lib\site-packages\rest_framework\mixins.py" in list
  45.             return self.get_paginated_response(serializer.data)
File "C:\Python27\lib\site-packages\rest_framework\serializers.py" in data
  663.         ret = super(ListSerializer, self).data
File "C:\Python27\lib\site-packages\rest_framework\serializers.py" in data
  228.                 self._data = self.to_representation(self.instance)
File "C:\Python27\lib\site-packages\rest_framework\serializers.py" in to_representation
  603.             self.child.to_representation(item) for item in iterable
File "C:\Python27\lib\site-packages\rest_framework\serializers.py" in to_representation
  461.                 ret[field.field_name] = field.to_representation(attribute)
File "C:\Python27\lib\site-packages\rest_framework\fields.py" in to_representation
  1450.         return [self.child.to_representation(item) for item in data]
File "C:\Python27\lib\site-packages\rest_framework\fields.py" in to_representation
  873.         return float(value)

Exception Type: TypeError at /requests/
Exception Value: float() argument must be a string or a number

pseudocode of django model field:

efficiencyGCE = ArrayField(verbose_name='GCE Efficiency',
                           base_field=models.FloatField(null=True, blank=True))

and the serializer for it:

class RequestSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Request

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy