开发者

How can I get the Django SplitDateTimeWidget to recognize AM/PM inputs?

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


You could just override django.contrib.admin.widgets.AdminSplitDateTime

This is the code:

class CustomAdminSplitDateTime(AdminSplitDateTime):
def __init__(self, attrs=None):
    widgets = [AdminDateWidget, AdminTimeWidget(attrs=None, format='%I:%M %p')]
    forms.MultiWidget.__init__(self, widgets, attrs)

Now you can use it as follows:

class MyForm(forms.ModelForm):

  class Meta:
    model=MyModel
    exclude=()
    start_datetime= forms.DateField(
        widget=CustomAdminSplitDateTime()
    )
    end_datetime= forms.DateField(
        widget=CustomAdminSplitDateTime()
    )

Don't forget to add the time format to the supported formats in your settings file, e.g.:

TIME_INPUT_FORMATS = [
    '%I:%M %p',
    '%H:%M:%S',
    '%H:%M:%S.%f',
    '%H:%M'
]


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')
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜