mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-22 17:47:04 +03:00
Merge pull request #2869 from ryangallen/datefield
DateField to_representation can handle str and empty values. Fixes #2656, #2687.
This commit is contained in:
commit
abcd6f2b9e
|
@ -925,6 +925,9 @@ class DateField(Field):
|
||||||
self.fail('invalid', format=humanized_format)
|
self.fail('invalid', format=humanized_format)
|
||||||
|
|
||||||
def to_representation(self, value):
|
def to_representation(self, value):
|
||||||
|
if not value:
|
||||||
|
return None
|
||||||
|
|
||||||
if self.format is None:
|
if self.format is None:
|
||||||
return value
|
return value
|
||||||
|
|
||||||
|
@ -938,7 +941,10 @@ class DateField(Field):
|
||||||
)
|
)
|
||||||
|
|
||||||
if self.format.lower() == ISO_8601:
|
if self.format.lower() == ISO_8601:
|
||||||
|
if (isinstance(value, str)):
|
||||||
|
value = datetime.datetime.strptime(value, '%Y-%m-%d').date()
|
||||||
return value.isoformat()
|
return value.isoformat()
|
||||||
|
|
||||||
return value.strftime(self.format)
|
return value.strftime(self.format)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -726,7 +726,10 @@ class TestDateField(FieldValues):
|
||||||
datetime.datetime(2001, 1, 1, 12, 00): ['Expected a date but got a datetime.'],
|
datetime.datetime(2001, 1, 1, 12, 00): ['Expected a date but got a datetime.'],
|
||||||
}
|
}
|
||||||
outputs = {
|
outputs = {
|
||||||
datetime.date(2001, 1, 1): '2001-01-01'
|
datetime.date(2001, 1, 1): '2001-01-01',
|
||||||
|
'2001-01-01': '2001-01-01',
|
||||||
|
None: None,
|
||||||
|
'': None,
|
||||||
}
|
}
|
||||||
field = serializers.DateField()
|
field = serializers.DateField()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user