I can't for the life of me figure out how to get the SplitDateTimeWidget
(link to ref doc) to recognize AM/PM inputs.
I've tried both the template-style input:
widgets={'start_datetime':forms.SplitDateTimeWidget(time_format='h:i A')}
and the standard Python time inputs (suggested here):
widgets={'start_datetime':forms.SplitDateTimeWidget(time_format='%I:%M %p')}
As you could guess, start_datetime
is a DateTimeField
in my model:
start_datetime=models.DateTimeField('Start', default=datetime.now())
Nothing I do seems to get this to pass validation -- I keep getting errors in that field. Any suggestions? I've been searching now for 2 hours to no avail. I can get it to validate if I stick with 24-hour time or if I leave out the AM/PM part, but I'd rather be able to correctly interpret AM/PM.
Thanks
Ultimately, after poking around a lot in the source for the SplitDateTimeWidget
, I didn't see a way to pass input_time_formats
into the widget. Perhaps there is a way (I don't understand the common lineage between fields and widgets well enough to say for sure one way or another), but I couldn't find it.
However, I was able to get it working by overriding the fields and using SplitDateTimeField
in the ModelForm (original fields in model are DateTime
type). I then needed to pass the DateTime
from the SplitDateTimeField
back to the original model, since the form field and the model field were unlinked since I overrode it.
class MyForm(forms.ModelForm):
start_datetime=forms.SplitDateTimeField(input_time_formats=['%I:%M %p'])
end_datetime=forms.SplitDateTimeField(input_time_formats=['%I:%M %p'])
class Meta:
model=MyModel
exclude=('start_datetime','end_datetime')
def clean(self):
self.instance.start_datetime=self.cleaned_data.get('start_datetime')
self.instance.end_datetime=self.cleaned_data.get('end_datetime')