0
我有兩個MultiWidget一個在另一個裏面,但問題是MultiWidget包含不返回壓縮,我該如何從第一個小部件獲取正確的值? 在這種情況下,從SplitTimeWidgetMultiWidget中的MultiWidget如何壓縮第一個?
class SplitTimeWidget(forms.MultiWidget):
"""
Widget written to split widget into hours and minutes.
"""
def __init__(self, attrs=None):
widgets = (
forms.Select(attrs=attrs, choices=([(hour,hour) for hour in range(0,24)])),
forms.Select(attrs=attrs, choices=([(minute, str(minute).zfill(2)) for minute in range(0,60)])),
)
super(SplitTimeWidget, self).__init__(widgets, attrs)
def decompress(self, value):
if value:
return [value.hour, value.minute]
return [None, None]
class DateTimeSelectWidget (forms.MultiWidget):
"""
A widget that splits date into Date and Hours, minutes, seconds with selects
"""
date_format = DateInput.format
def __init__(self, attrs=None, date_format=None):
if date_format:
self.date_format = date_format
#if time_format:
# self.time_format = time_format
hours = [(hour,str(hour)+' h') for hour in range(0,24)]
minutes = [(minute,minute) for minute in range(0,60)]
seconds = minutes #not used always in 0s
widgets = (
DateInput(attrs=attrs, format=self.date_format),
SplitTimeWidget(attrs=attrs),
)
super(DateTimeSelectWidget,self).__init__(widgets, attrs)
def decompress(self, value):
if value:
return [value.date(), value.time()]
else:
[None, None, None]
事實上,我正在尋找1xTextInput(日期)和2xSelect(小時,分鐘),我已經準備好完成了,事實是我不喜歡重複我的自我,但無論如何它已經完成。 如果你把日期放在select中,那麼如何計算呢?我的意思是在正確的月份顯示28,30和31天的天數? JavaScript的? – sacabuche 2010-04-19 12:15:01
我會用django/forms/extras/widgets.py作爲起點。如果用戶選擇選擇無效日期(例如2010/02/30),則表單字段將引發通常的ValidationError。我從來不打算爲這種情況實現一些JavaScript。 – mawimawi 2010-04-22 09:03:47