Skip to content

Fix DateField.to_representation to work with Python 2 unicode. #3819

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion rest_framework/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
value = datetime.datetime.strptime(value, '%Y-%m-%d').date()
return value.isoformat()

Expand Down
3 changes: 2 additions & 1 deletion tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import django
import pytest
from django.http import QueryDict
from django.utils import timezone
from django.utils import six, timezone

import rest_framework
from rest_framework import serializers
Expand Down Expand Up @@ -895,6 +895,7 @@ class TestDateField(FieldValues):
outputs = {
datetime.date(2001, 1, 1): '2001-01-01',
'2001-01-01': '2001-01-01',
six.text_type('2016-01-10'): '2016-01-10',
None: None,
'': None,
}
Expand Down