0

我有两个 MultiWidget 一个在另一个里面,但问题是包含的 MultiWidget 不返回压缩,我该如何从第一个小部件中获取正确的值?在这种情况下,来自 SplitTimeWidget

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]
4

1 回答 1

1

我将只创建一个大的 MultiWidget - 像 DateSelectTimeSplitWidget 这样的东西,它使用您需要的所有标准小部件。(3 个选择,2 个输入)。毕竟它将被存储到一个时间戳字段中,对吧?

于 2010-04-18T19:37:44.537 回答