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 DateField.to_representation to work with Python 2 unicode. #3819

Conversation

m1kola
Copy link
Contributor

@m1kola m1kola commented Jan 10, 2016

Hello!

Seems like DateField.to_representation doesn't work with unicode values on Python 2.*, but it can accept str values. I think it should work with unicode too.

Here is example:

from rest_framework import serializers.

class Example(object):
    def __init__(self, created):
        self.created = created

class ExampleSerializer(serializers.Serializer):
    created = serializers.DateTimeField()

example = Example(created=u'2016-01-10')
es = ExampleSerializer(example)
print(es.data)

Traceback:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/Users/m1kola/.virtualenvs/drf_dev/lib/python2.7/site-packages/rest_framework/serializers.py", line 503, in data
    ret = super(Serializer, self).data
  File "/Users/m1kola/.virtualenvs/drf_dev/lib/python2.7/site-packages/rest_framework/serializers.py", line 239, in data
    self._data = self.to_representation(self.instance)
  File "/Users/m1kola/.virtualenvs/drf_dev/lib/python2.7/site-packages/rest_framework/serializers.py", line 472, in to_representation
    ret[field.field_name] = field.to_representation(attribute)
  File "/Users/m1kola/.virtualenvs/drf_dev/lib/python2.7/site-packages/rest_framework/fields.py", line 1068, in to_representation
    value = value.isoformat()
AttributeError: 'unicode' object has no attribute 'isoformat'

I can reproduce this behaviour on 3.3.1 and master.

@m1kola m1kola force-pushed the bug/DateField-to_representation-unicode-compatibility branch 3 times, most recently from d474afc to 82d83fc Compare Jan 10, 2016
@m1kola m1kola changed the title DateField.to_representation can't work with unicode value PY2: DateField.to_representation can't work with unicode value Jan 10, 2016
@jpadilla
Copy link
Member

jpadilla commented Jan 11, 2016

I think this is a pretty valid enhancement. We could probably just test for six.string_types. #3809 could probably also do with this.

@@ -1139,7 +1139,7 @@ def to_representation(self, value):
)

if output_format.lower() == ISO_8601:
if (isinstance(value, str)):
if isinstance(value, (six.string_types, six.text_type)):
Copy link
Collaborator

@xordoquy xordoquy Jan 11, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

string_types is enough, you don't need the test_type here

@m1kola m1kola force-pushed the bug/DateField-to_representation-unicode-compatibility branch from 82d83fc to 6b207d9 Compare Jan 11, 2016
@m1kola
Copy link
Contributor Author

m1kola commented Jan 11, 2016

PR was updated according comments.

@xordoquy xordoquy added this to the 3.3.3 Release milestone Jan 11, 2016
xordoquy added a commit that referenced this pull request Jan 11, 2016
…unicode-compatibility

PY2: DateField.to_representation can't work with unicode value
@xordoquy xordoquy merged commit edd9c7d into encode:master Jan 11, 2016
2 checks passed
@xordoquy
Copy link
Collaborator

xordoquy commented Jan 11, 2016

Thanks !

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

Successfully merging this pull request may close these issues.

None yet

3 participants