1
0
mirror of https://github.com/django/django.git synced 2025-10-31 09:41:08 +00:00

Fixed #20986 -- Enabled SelectDateWidget to use custom months

Reviewed by Trac alias MarkusH.
This commit is contained in:
Loic Bistuer
2013-08-28 13:22:47 +07:00
committed by Anssi Kääriäinen
parent b89c2a5d9e
commit da800be6dd
4 changed files with 98 additions and 6 deletions

View File

@@ -51,16 +51,23 @@ class SelectDateWidget(Widget):
day_field = '%s_day'
year_field = '%s_year'
def __init__(self, attrs=None, years=None, required=True):
# years is an optional list/tuple of years to use in the "year" select box.
def __init__(self, attrs=None, years=None, required=True, months=None):
self.attrs = attrs or {}
self.required = required
# Optional list or tuple of years to use in the "year" select box.
if years:
self.years = years
else:
this_year = datetime.date.today().year
self.years = range(this_year, this_year+10)
# Optional dict of months to use in the "month" select box.
if months:
self.months = months
else:
self.months = MONTHS
def render(self, name, value, attrs=None):
try:
year_val, month_val, day_val = value.year, value.month, value.day
@@ -80,7 +87,7 @@ class SelectDateWidget(Widget):
year_val, month_val, day_val = [int(v) for v in match.groups()]
choices = [(i, i) for i in self.years]
year_html = self.create_select(name, self.year_field, value, year_val, choices)
choices = list(six.iteritems(MONTHS))
choices = list(six.iteritems(self.months))
month_html = self.create_select(name, self.month_field, value, month_val, choices)
choices = [(i, i) for i in range(1, 32)]
day_html = self.create_select(name, self.day_field, value, day_val, choices)