Skip to content

Instantly share code, notes, and snippets.

@kmike
Created March 16, 2012 11:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kmike/2049651 to your computer and use it in GitHub Desktop.
Save kmike/2049651 to your computer and use it in GitHub Desktop.
SplitDateField
class SplitDateInput(forms.MultiWidget):
"""
Виджет для ввода даты с 3 полями.
"""
def __init__(self, attrs=None):
widgets = [
forms.TextInput(attrs=attrs), # день
forms.Select(attrs=attrs, choices=MONTHS.items()), # месяц
forms.TextInput(attrs=attrs), # год
]
super(SplitDateInput, self).__init__(widgets, attrs)
def decompress(self, value):
if value:
return [value.day, value.month, value.year]
return [None, None, None]
class SplitDateField(forms.MultiValueField):
"""
Поля для ввода даты из 3 полей
"""
widget = SplitDateInput
def __init__(self, *args, **kwargs):
fields = (
forms.IntegerField(min_value=1, max_value=31),
forms.IntegerField(min_value=1, max_value=12),
forms.IntegerField(min_value=1900, max_value=2020),
)
super(SplitDateField, self).__init__(fields, *args, **kwargs)
def compress(self, data_list):
if data_list:
day, month, year = data_list
if not day or not month or not year:
raise ValidationError(u'Дата - неправильная')
try:
result = datetime.date(year, month, day)
except ValueError:
raise ValidationError(u'Нет такой даты')
return result
return None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment