Skip to content

Pass through strings as-in with date/time fields. #4196

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
merged 1 commit into from
Jun 14, 2016
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
6 changes: 2 additions & 4 deletions rest_framework/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -1073,7 +1073,7 @@ def to_representation(self, value):

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

if output_format is None:
if output_format is None or isinstance(value, six.string_types):
return value

if output_format.lower() == ISO_8601:
Expand Down Expand Up @@ -1133,7 +1133,7 @@ def to_representation(self, value):

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

if output_format is None:
if output_format is None or isinstance(value, six.string_types):
return value

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

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

return value.strftime(output_format)
Expand Down
2 changes: 2 additions & 0 deletions tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -993,6 +993,8 @@ class TestDateTimeField(FieldValues):
outputs = {
datetime.datetime(2001, 1, 1, 13, 00): '2001-01-01T13:00:00',
datetime.datetime(2001, 1, 1, 13, 00, tzinfo=timezone.UTC()): '2001-01-01T13:00:00Z',
'2001-01-01T00:00:00': '2001-01-01T00:00:00',
six.text_type('2016-01-10T00:00:00'): '2016-01-10T00:00:00',
None: None,
'': None,
}
Expand Down
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