Merge pull request #708 from kevinastone/master

Fixed DateField with Empty Values (Fixes #707)
This commit is contained in:
Tom Christie 2013-03-07 01:01:07 -08:00
commit f5a8f172a3
2 changed files with 18 additions and 0 deletions

View File

@ -534,6 +534,8 @@ class DateField(WritableField):
raise ValidationError(msg)
def to_native(self, value):
if value is None:
return None
if isinstance(value, datetime.datetime):
value = value.date()
if self.format.lower() == ISO_8601:
@ -599,6 +601,8 @@ class DateTimeField(WritableField):
raise ValidationError(msg)
def to_native(self, value):
if value is None:
return None
if self.format.lower() == ISO_8601:
return value.isoformat()
return value.strftime(self.format)

View File

@ -171,6 +171,13 @@ class DateFieldTest(TestCase):
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):
"""
@ -303,6 +310,13 @@ class DateTimeFieldTest(TestCase):
self.assertEqual('1984 - 04:31', result_3)
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):
"""