mediumgrade
unread,Nov 21, 2009, 8:01:30 PM11/21/09Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to Django users
I am trying to create my own Dojo-aware Django widgets. I have defined
the following
class DojoDateWidget(forms.widgets.DateTimeInput):
format = '%Y-%m-%d'
def __init__(self, *args, **kwargs):
kwargs.setdefault('attrs', {}).update({'dojoType':
"dijit.form.DateTextBox",
'required': kwargs.pop
('required', False),
'constraints': mark_safe
("{datePattern:'MM/dd/yyyy'}"),
})
attrs = kwargs['attrs']
if kwargs.get('promptMessage', None) is not None:
attrs['promptMessage'] = kwargs.pop('promptMessage')
super(DojoDateWidget, self).__init__(*args, **kwargs)
class DojoTimeWidget(forms.TimeInput):
input_type = 'text'
def __init__(self, *args, **kwargs):
kwargs.setdefault('attrs', {}).update({'dojoType':
"dijit.form.TimeTextBox",
'required': kwargs.pop
('required', False),
#'constraints': mark_safe
("{timePattern:'HH:mm'}"),
})
attrs = kwargs['attrs']
if kwargs.get('promptMessage', None) is not None:
attrs['promptMessage'] = kwargs.pop('promptMessage')
self.input_formats = ['T%H:%M:%S']
super(DojoTimeWidget, self).__init__(*args, **kwargs)
def render(self, name, value, attrs=None):
if value is None:
value = ''
elif isinstance(value, time):
value = value.replace(microsecond=0)
#For Dojo TimeTextBox formatting.
#Requires T14:30:59 (there's a 'T' in front)
value = 'T%s' % value
return super(DojoTimeWidget, self).render(name, value, attrs)
class DojoDateTimeWidget(forms.widgets.MultiWidget):
def __init__(self, attrs=None):
widgets = (DojoDateWidget, DojoTimeWidget)
super(DojoDateTimeWidget, self).__init__(widgets, attrs)
def decompress(self, value):
if value:
return [value.date(), value.time().replace(microsecond=0)]
return [None, None]
The DojoDateWidget and DojoTimeWidget validate correctly when
submitting in a form. However, when I leave the DojoDateTimeWidget
blank in a form, Django sees the empty values as ['', ''] as opposed
to simply None which causes the form validation to say that the date
time format is invalid. I see that the decompressed null value for the
widget is "return [None, None]" but this is the same as the Django
built-in SplitDateTimeWidget and when I simply return "None" I get the
error message "'NoneType' object is unsubscriptable". How do I get my
datetime widget to correctly send an empty value?