From 32586b0ba43816d325be0ce807f75623683eed7a Mon Sep 17 00:00:00 2001 From: Guillaume Pannatier Date: Fri, 23 May 2014 09:07:15 +0200 Subject: [PATCH] Fixed #22684 -- Added `empty_label` option on `django.forms.extras.widets.SelectDateWidget` Thanks danielsamuels for the report --- AUTHORS | 1 + django/forms/extras/widgets.py | 5 ++++- docs/ref/forms/widgets.txt | 12 ++++++++++++ docs/releases/1.8.txt | 4 ++++ tests/forms_tests/tests/test_extra.py | 5 +++++ 5 files changed, 26 insertions(+), 1 deletion(-) diff --git a/AUTHORS b/AUTHORS index 5eed297956..534e3e0f52 100644 --- a/AUTHORS +++ b/AUTHORS @@ -484,6 +484,7 @@ answer newbie questions, and generally made Django that much better: oggy Tomek Paczkowski Jens Page + Guillaume Pannatier Jay Parlar Carlos Eduardo de Paula John Paulett diff --git a/django/forms/extras/widgets.py b/django/forms/extras/widgets.py index ea26cbe4e4..09d22b0fb5 100644 --- a/django/forms/extras/widgets.py +++ b/django/forms/extras/widgets.py @@ -48,7 +48,7 @@ class SelectDateWidget(Widget): day_field = '%s_day' year_field = '%s_year' - def __init__(self, attrs=None, years=None, months=None): + def __init__(self, attrs=None, years=None, months=None, empty_label=None): self.attrs = attrs or {} # Optional list or tuple of years to use in the "year" select box. @@ -64,6 +64,9 @@ class SelectDateWidget(Widget): else: self.months = MONTHS + if empty_label is not None: + self.none_value = (0, empty_label) + def render(self, name, value, attrs=None): try: year_val, month_val, day_val = value.year, value.month, value.day diff --git a/docs/ref/forms/widgets.txt b/docs/ref/forms/widgets.txt index 5569e38b47..cf309f0298 100644 --- a/docs/ref/forms/widgets.txt +++ b/docs/ref/forms/widgets.txt @@ -781,3 +781,15 @@ Composite widgets 5:_('may'), 6:_('jun'), 7:_('jul'), 8:_('aug'), 9:_('sep'), 10:_('oct'), 11:_('nov'), 12:_('dec') } + + .. attribute:: SelectDateWidget.empty_label + + .. versionadded:: 1.8 + + If the :class:`~django.forms.DateField` is not required, + :class:`SelectDateWidget` will have an empty choice at the top of + the list. You can change the text of this label + (which is ``---`` by default) with the ``empty_label`` attribute:: + + # A custom empty label + field1 = forms.DateField(widget=SelectDateWidget(empty_label="Nothing")) diff --git a/docs/releases/1.8.txt b/docs/releases/1.8.txt index b0e3b035c0..2fed9fa4a6 100644 --- a/docs/releases/1.8.txt +++ b/docs/releases/1.8.txt @@ -136,6 +136,10 @@ Forms a form's :attr:`~django.forms.Form.label_suffix` while using shortcuts such as ``{{ form.as_p }}`` in templates. +* :class:`~django.forms.extras.widgets.SelectDateWidget` now accepts an + :attr:`~django.forms.extras.widgets.SelectDateWidget.empty_label` argument, which will + override the top list choice label when :class:`~django.forms.DateField` is not required. + Internationalization ^^^^^^^^^^^^^^^^^^^^ diff --git a/tests/forms_tests/tests/test_extra.py b/tests/forms_tests/tests/test_extra.py index f74a6a08b2..499db79014 100644 --- a/tests/forms_tests/tests/test_extra.py +++ b/tests/forms_tests/tests/test_extra.py @@ -297,6 +297,11 @@ class FormsExtraTestCase(TestCase, AssertFormErrorsMixin): """) + w = SelectDateWidget(years=('2014',), empty_label='empty_label') + + # Rendering the default state with empty_label setted. + self.assertInHTML('', w.render('mydate', ''), count=3) + a = GetDate({'mydate_month': '4', 'mydate_day': '1', 'mydate_year': '2008'}) self.assertTrue(a.is_valid()) self.assertEqual(a.cleaned_data['mydate'], datetime.date(2008, 4, 1))