diff --git a/django/forms/extras/widgets.py b/django/forms/extras/widgets.py index 4fb4869e689..65d6bf15f5d 100644 --- a/django/forms/extras/widgets.py +++ b/django/forms/extras/widgets.py @@ -136,6 +136,9 @@ class SelectDateWidget(Widget): return select_html def _has_changed(self, initial, data): - input_format = get_format('DATE_INPUT_FORMATS')[0] - data = datetime_safe.datetime.strptime(data, input_format).date() - return super(SelectDateWidget, self)._has_changed(initial, data) \ No newline at end of file + try: + input_format = get_format('DATE_INPUT_FORMATS')[0] + data = datetime_safe.datetime.strptime(data, input_format).date() + except (TypeError, ValueError): + pass + return super(SelectDateWidget, self)._has_changed(initial, data) diff --git a/tests/regressiontests/forms/tests/extra.py b/tests/regressiontests/forms/tests/extra.py index 3c5dfabb615..c873af7a8ca 100644 --- a/tests/regressiontests/forms/tests/extra.py +++ b/tests/regressiontests/forms/tests/extra.py @@ -18,6 +18,9 @@ from .error_messages import AssertFormErrorsMixin class GetDate(Form): mydate = DateField(widget=SelectDateWidget) +class GetNotRequiredDate(Form): + mydate = DateField(widget=SelectDateWidget, required=False) + class GetDateShowHiddenInitial(Form): mydate = DateField(widget=SelectDateWidget, show_hidden_initial=True) @@ -619,6 +622,15 @@ class FormsExtraTestCase(TestCase, AssertFormErrorsMixin): self.assertTrue(FormWithFile().is_multipart()) self.assertTrue(FormWithImage().is_multipart()) + def test_field_not_required(self): + b = GetNotRequiredDate({ + 'mydate_year': '', + 'mydate_month': '', + 'mydate_day': '' + }) + self.assertFalse(b.has_changed()) + + class FormsExtraL10NTestCase(TestCase): def setUp(self):