Skip to content

Commit 9406e45

Browse files
authored
Pass through strings as-in with DateTimeField (#4196)
1 parent 1633a0a commit 9406e45

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

rest_framework/fields.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,7 +1073,7 @@ def to_representation(self, value):
10731073

10741074
output_format = getattr(self, 'format', api_settings.DATETIME_FORMAT)
10751075

1076-
if output_format is None:
1076+
if output_format is None or isinstance(value, six.string_types):
10771077
return value
10781078

10791079
if output_format.lower() == ISO_8601:
@@ -1133,7 +1133,7 @@ def to_representation(self, value):
11331133

11341134
output_format = getattr(self, 'format', api_settings.DATE_FORMAT)
11351135

1136-
if output_format is None:
1136+
if output_format is None or isinstance(value, six.string_types):
11371137
return value
11381138

11391139
# Applying a `DateField` to a datetime value is almost always
@@ -1146,8 +1146,6 @@ def to_representation(self, value):
11461146
)
11471147

11481148
if output_format.lower() == ISO_8601:
1149-
if isinstance(value, six.string_types):
1150-
value = datetime.datetime.strptime(value, '%Y-%m-%d').date()
11511149
return value.isoformat()
11521150

11531151
return value.strftime(output_format)

tests/test_fields.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -993,6 +993,8 @@ class TestDateTimeField(FieldValues):
993993
outputs = {
994994
datetime.datetime(2001, 1, 1, 13, 00): '2001-01-01T13:00:00',
995995
datetime.datetime(2001, 1, 1, 13, 00, tzinfo=timezone.UTC()): '2001-01-01T13:00:00Z',
996+
'2001-01-01T00:00:00': '2001-01-01T00:00:00',
997+
six.text_type('2016-01-10T00:00:00'): '2016-01-10T00:00:00',
996998
None: None,
997999
'': None,
9981000
}

0 commit comments

Comments
 (0)
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