Pass through strings as-in with DateTimeField (#4196)

This commit is contained in:
Tom Christie 2016-06-14 12:23:39 +01:00 committed by GitHub
parent 1633a0a2b1
commit 9406e45b2c
2 changed files with 4 additions and 4 deletions

View File

@ -1073,7 +1073,7 @@ class DateTimeField(Field):
output_format = getattr(self, 'format', api_settings.DATETIME_FORMAT) 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 return value
if output_format.lower() == ISO_8601: if output_format.lower() == ISO_8601:
@ -1133,7 +1133,7 @@ class DateField(Field):
output_format = getattr(self, 'format', api_settings.DATE_FORMAT) 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 return value
# Applying a `DateField` to a datetime value is almost always # Applying a `DateField` to a datetime value is almost always
@ -1146,8 +1146,6 @@ class DateField(Field):
) )
if output_format.lower() == ISO_8601: 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.isoformat()
return value.strftime(output_format) return value.strftime(output_format)

View File

@ -993,6 +993,8 @@ class TestDateTimeField(FieldValues):
outputs = { outputs = {
datetime.datetime(2001, 1, 1, 13, 00): '2001-01-01T13:00:00', 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', 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: None,
'': None, '': None,
} }