import datetime from django import newforms as forms class DateDropdownWidget(forms.MultiWidget): def __init__(self,attrs=None,year_range=None,month_range=None,day_range=None): YEARS = year_range or range(2000,2021) MONTHES = month_range or range(1,13) DAYS = day_range or range(1,32) years = map( lambda x: (x,x), YEARS ) months = map(lambda x:(x,x), MONTHES ) days = map( lambda x: (x,x), DAYS ) widgets = ( forms.Select(choices=years), forms.Select(choices=months), forms.Select(choices=days), ) super(DateDropdownWidget, self).__init__(widgets, attrs) def format_output(self,widgets): format = "Year %s Month %s Day %s".decode('utf-8') return format%(widgets[0],widgets[1],widgets[2]) def decompress(self,value): if value: return [value.year, value.month,value.day] return [None,None,None] class DateField(forms.MultiValueField): widget = DateDropdownWidget def __init__(self,*args,**kwargs): fields = ( forms.IntegerField( required=True), forms.IntegerField( required=True), forms.IntegerField( required=True ), ) super(DateField, self).__init__(fields, *args,**kwargs ) def compress(self,data_list): EMPTY_VALUES = [None, ''] ERROR_EMPTY = "Fill the fields." ERROR_INVALID = "Enter a valid date." if data_list: if filter(lambda x: x in EMPTY_VALUES, data_list): raise forms.ValidationError(ERROR_EMPTY) try: return datetime.datetime(*map(lambda x:int(x),data_list)) except ValueError: raise forms.ValidationError(ERROR_INVALID) return None