0

I have two MultiWidget one inside the other, but the problem is that the MultiWidget contained don't return compress, how do i do to get the right value from the first widget? In this case from 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]
sacabuche
  • 2,781
  • 1
  • 27
  • 35

1 Answers1

1

I would create just one big MultiWidget - something like DateSelectTimeSplitWidget which uses all standard widgets that you need. (3xSelect, 2xInput). After all it will be stored into one Timestamp field, right?

mawimawi
  • 4,222
  • 3
  • 33
  • 52
  • In fact i'm looking for 1xTextInput(date) and 2xSelect(hours,minutes), and i all ready finished, the fact is that i did not like to repeat my self, but anyway it's done. How are yoy going to do if you put the date in select?, i mean display 28, 30 and 31 number of days in the correct month? javascript? – sacabuche Apr 19 '10 at 12:15
  • I would use django/forms/extras/widgets.py as a starting point. if a user chooses to select an invalid date (e.g. 2010/02/30), then the form field will raise the usual ValidationError. I never bothered to implement some javascript for this case. – mawimawi Apr 22 '10 at 09:03