I have a trouble with newforms lib and MultiWidget.
I created a custom MultiWidget based on SplitDateTimeWidget.
The problem is that when I run the form.is_valid()
The value send to the DateField clean method is a list ['1', '1',
'2000'].
Then the strptime command in this function failed with the following
message "TypeError: expected string or buffer"
I understand that strptime needs a string, but how can I validate my
data ? Is there someting special to do before "cleaning" the form with
MultiWidget based fields ?
I tried to update the POST dict with something like that before
"cleaning" the form, but of course this is not working ;)
data.update({'BirthDate':'%s-%s-%s' %
(data['BirthDate_2'],data['BirthDate_1'],data['BirthDate_0'])})
del data['BirthDate_2']
del data['BirthDate_1']
del data['BirthDate_0']
Here is the POST data dict :
<MultiValueDict: {'BirthDate_1': ['1'], 'BirthDate_0': ['1'],
'FirstName': [''], 'BirthDate_2': ['2000'], 'LastName': ['']}>
Here is the form and widget code :
---------------------------------------------------------
class DropDownDateWidget(MultiWidget):
def __init__(self, attrs=None):
widgets = (forms.Select(choices=DAY_CHOICES),
forms.Select(choices=MONTH_CHOICES),
forms.TextInput(attrs=attrs))
super(DropDownDateWidget, self).__init__(widgets, attrs)
def decompress(self, value):
if value:
return [value.day, value.month, value.year]
return [None, None, None]
class InfoForm(forms.Form):
FirstName =
forms.CharField(required=True,max_length=100,help_text="help
FirstName")
LastName =
forms.CharField(required=True,max_length=100,help_text="help
LastName")
BirthDate =
forms.DateField(required=False,widget=DropDownDateWidget(),help_text="help
BirthDate")
---------------------------------------------------------
Thanks in advance
xav
The answer is ...
... implementing a custom value_from_datadict method for the class
DropDownDateWidget(MultiWidget)
xav