Best way to let Django SplitDateTimeField accept an empty time field?
I like to have the date part in the SplitDateField, but the time part - if left blank - defaults to some value ('00: 00: 00 ').
I am currently a monkey patch that is used in the Fields clean () method. Is there a better way to do this? A more "Django-like" way?
Looks like this now (in forms.py file):
def mp_clean(self, value):
# time is in value[1] of a split-datetime field:
field_value = value[1]
if field_value in self.empty_values:
value[1] = '00:00:00'
return self.mp_original_clean(value)
class MyModelAdminForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(MyModelAdminForm, self).__init__(*args, **kwargs)
that_field = self.fields['that_field']
if isinstance(that_field,forms.SplitDateTimeField):
that_field.mp_original_clean = that_field.clean
that_field.clean = types.MethodType(mp_clean, that_field)
+3
source to share