Hiya,
I've been mucking about with the SplitDateTimeWidget field, and it didn't
really work for me - it wouldn't accept "None", even when required was set to
"False", and it needed a "set to now" button :-)
Feel free to criticise the code, and have fun!
_now_button = """
<input type="button" value="Set to now" onClick="
%(target_name)s_0.value = '%(date)s';
%(target_name)s_1.value = '%(hour)d:%(minute)d';
"
/>
"""
class SplitDateTimeResetWidget(MultiWidget):
time_field = '%s_time'
month_field = '%s_month'
def __init__(self, attrs=None):
widgets = (TextInput(attrs=attrs), TextInput(attrs=attrs))
super(SplitDateTimeResetWidget, self).__init__(widgets, attrs)
def decompress(self, value):
if value:
print "Returning " + value.date() + ":" + value.time()
return [value.date(), value.time()]
return None
def render(self, name, value, attrs=None):
if not isinstance(value, list):
value = self.decompress(value)
output = []
for i, widget in enumerate(self.widgets):
try:
widget_value = value[i]
except KeyError:
widget_value = None
except TypeError:
widget_value = None
output.append(widget.render(name + '_%s' % i, widget_value, attrs))
now = datetime.datetime.now()
output.append(_now_button % { 'target_name': name, 'date': now.date(),
'hour': now.hour, 'minute': now.minute } )
return self.format_output(output)
class SplitDateTimeResetField(MultiValueField):
widget = SplitDateTimeResetWidget
def __init__(self, required=True, widget=None, label=None, initial=None):
fields = (DateField(), TimeField())
super(SplitDateTimeResetField, self).__init__(fields, required, widget,
label, initial)
def compress(self, data_list):
if data_list[0] and data_list[1]:
return datetime.datetime.combine(*data_list)
return None
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---