[1.8.x] Fixed #24339 -- Fixed crash with empty DurationField form field.

Backport of 8a21d25033 from master
This commit is contained in:
Michael Angeletti 2015-02-13 17:12:23 -05:00 committed by Tim Graham
parent 0f3eb8260b
commit 2e6d8e51db
2 changed files with 12 additions and 2 deletions

View File

@ -526,7 +526,8 @@ class DurationField(Field):
}
def prepare_value(self, value):
return duration_string(value)
if isinstance(value, datetime.timedelta):
return duration_string(value)
def to_python(self, value):
if value in self.empty_values:

View File

@ -48,6 +48,7 @@ from django.test import SimpleTestCase, ignore_warnings
from django.utils import formats, six, translation
from django.utils._os import upath
from django.utils.deprecation import RemovedInDjango20Warning
from django.utils.duration import duration_string
try:
from PIL import Image
@ -607,7 +608,7 @@ class FieldsTests(SimpleTestCase):
d = datetime.datetime(2006, 9, 17, 14, 30, 0)
self.assertFalse(f.has_changed(d, '2006 09 17 2:30 PM'))
# RegexField ##################################################################
# DurationField ###########################################################
def test_durationfield_1(self):
f = DurationField()
@ -635,6 +636,14 @@ class FieldsTests(SimpleTestCase):
str(f['duration'])
)
def test_durationfield_prepare_value(self):
field = DurationField()
td = datetime.timedelta(minutes=15, seconds=30)
self.assertEqual(field.prepare_value(td), duration_string(td))
self.assertIsNone(field.prepare_value(None))
# RegexField ##################################################################
def test_regexfield_1(self):
f = RegexField('^[0-9][A-F][0-9]$')
self.assertEqual('2A2', f.clean('2A2'))