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

Fix serialization of properties holding None values #85

Merged
merged 1 commit into from Oct 29, 2015

Conversation

nmandery
Copy link
Contributor

The current implementation fails to serialize properties holding the value None. This may not affect al types of field as it depends how the field handles its conversion in its Field.to_representation() method. At least with datetime fields the attempt to serialize a None value will result in an AttributeError -- see the following stacktrace:

  [...]
  File "/home/nico/environments/venv-django17/local/lib/python2.7/site-packages/rest_framework/serializers.py", line 466, in data
    ret = super(Serializer, self).data
  File "/home/nico/environments/venv-django17/local/lib/python2.7/site-packages/rest_framework/serializers.py", line 213, in data
    self._data = self.to_representation(self.instance)
  File "/home/nico/environments/venv-django17/local/lib/python2.7/site-packages/rest_framework/serializers.py", line 435, in to_representation
    ret[field.field_name] = field.to_representation(attribute)
  File "/home/nico/environments/venv-django17/local/lib/python2.7/site-packages/rest_framework_gis/serializers.py", line 27, in to_representation
    ("features", super(GeoFeatureModelListSerializer, self).to_representation(data))
  File "/home/nico/environments/venv-django17/local/lib/python2.7/site-packages/rest_framework/serializers.py", line 568, in to_representation
    self.child.to_representation(item) for item in iterable
  File "/home/nico/environments/venv-django17/local/lib/python2.7/site-packages/rest_framework_gis/serializers.py", line 125, in to_representation
    feature["properties"] = self.get_properties(instance, fields)
  File "/home/nico/environments/venv-django17/local/lib/python2.7/site-packages/rest_framework_gis/serializers.py", line 148, in get_properties
    properties[field.field_name] = field.to_representation(value)
  File "/home/nico/environments/venv-django17/local/lib/python2.7/site-packages/rest_framework/fields.py", line 883, in to_representation
    return value.strftime(self.format)
AttributeError: 'NoneType' object has no attribute 'strftime'

This pull request addresses this issue by calling the to_representation method only when there is an actual value to convert.

@landscape-bot
Copy link

Code Health
Code quality remained the same when pulling b02f8c7 on nmandery:feature/serialize-none-values into 73ac3c0 on djangonauts:master.

@nemesifier nemesifier merged commit b02f8c7 into openwisp:master Oct 29, 2015
@nemesifier
Copy link
Member

thx @nmandery

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

Successfully merging this pull request may close these issues.

None yet

3 participants