mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-11 04:07:39 +03:00
Merge pull request #708 from kevinastone/master
Fixed DateField with Empty Values (Fixes #707)
This commit is contained in:
commit
f5a8f172a3
|
@ -534,6 +534,8 @@ class DateField(WritableField):
|
||||||
raise ValidationError(msg)
|
raise ValidationError(msg)
|
||||||
|
|
||||||
def to_native(self, value):
|
def to_native(self, value):
|
||||||
|
if value is None:
|
||||||
|
return None
|
||||||
if isinstance(value, datetime.datetime):
|
if isinstance(value, datetime.datetime):
|
||||||
value = value.date()
|
value = value.date()
|
||||||
if self.format.lower() == ISO_8601:
|
if self.format.lower() == ISO_8601:
|
||||||
|
@ -599,6 +601,8 @@ class DateTimeField(WritableField):
|
||||||
raise ValidationError(msg)
|
raise ValidationError(msg)
|
||||||
|
|
||||||
def to_native(self, value):
|
def to_native(self, value):
|
||||||
|
if value is None:
|
||||||
|
return None
|
||||||
if self.format.lower() == ISO_8601:
|
if self.format.lower() == ISO_8601:
|
||||||
return value.isoformat()
|
return value.isoformat()
|
||||||
return value.strftime(self.format)
|
return value.strftime(self.format)
|
||||||
|
|
|
@ -171,6 +171,13 @@ class DateFieldTest(TestCase):
|
||||||
|
|
||||||
self.assertEqual('1984 - 07.31', result_1)
|
self.assertEqual('1984 - 07.31', result_1)
|
||||||
|
|
||||||
|
def test_to_native_none(self):
|
||||||
|
"""
|
||||||
|
Make sure from_native() returns None on None param.
|
||||||
|
"""
|
||||||
|
f = serializers.DateField(required=False)
|
||||||
|
self.assertEqual(None, f.to_native(None))
|
||||||
|
|
||||||
|
|
||||||
class DateTimeFieldTest(TestCase):
|
class DateTimeFieldTest(TestCase):
|
||||||
"""
|
"""
|
||||||
|
@ -303,6 +310,13 @@ class DateTimeFieldTest(TestCase):
|
||||||
self.assertEqual('1984 - 04:31', result_3)
|
self.assertEqual('1984 - 04:31', result_3)
|
||||||
self.assertEqual('1984 - 04:31', result_4)
|
self.assertEqual('1984 - 04:31', result_4)
|
||||||
|
|
||||||
|
def test_to_native_none(self):
|
||||||
|
"""
|
||||||
|
Make sure from_native() returns None on None param.
|
||||||
|
"""
|
||||||
|
f = serializers.DateTimeField(required=False)
|
||||||
|
self.assertEqual(None, f.to_native(None))
|
||||||
|
|
||||||
|
|
||||||
class TimeFieldTest(TestCase):
|
class TimeFieldTest(TestCase):
|
||||||
"""
|
"""
|
||||||
|
|
Loading…
Reference in New Issue
Block a user