django中AdminTimeWidget中的时间输入格式

时间:2012-02-12 12:33:57

标签: python django django-admin django-forms

我使用django形式的管理日历和时间小部件:description, how to use them

但是窗口小部件输入时间格式为:“%H:%M:%S”。我需要“%H:%M”。这是我的代码:

class CorporateOrderForm(ModelForm):
    class Meta:
        model=Order
        #exclude=('giving_address_comment','giving_address','comment')
    def __init__(self, *args, **kwargs):
        super(CorporateOrderForm, self).__init__(*args, **kwargs)
        self.fields['order_date'].widget = widgets.AdminDateWidget()
        self.fields['order_time'].widget = widgets.AdminTimeWidget(format='%H:%M')

最后一行很重要:我尝试更改格式。这是AdminTimeWidget代码:

class AdminTimeWidget(forms.TimeInput):
    class Media:
        js = (settings.ADMIN_MEDIA_PREFIX + "js/calendar.js",
              settings.ADMIN_MEDIA_PREFIX + "js/admin/DateTimeShortcuts.js")

    def __init__(self, attrs={}, format=None):
        super(AdminTimeWidget, self).__init__(attrs={'class': 'vTimeField', 'size': '8'}, format=format)

这是TimeInput代码:

class TimeInput(Input):
    input_type = 'text'
    format = '%H:%M:%S'     # '14:30:59'

    def __init__(self, attrs=None, format=None):
      super(TimeInput, self).__init__(attrs)
      if format:
          self.format = format
          self.manual_format = True
      else:
          self.format = formats.get_format('TIME_INPUT_FORMATS')[0]
          self.manual_format = False

这不起作用,时间格式没有改变。错误在哪里?...

1 个答案:

答案 0 :(得分:0)

这样做:

class CorporateOrderForm(ModelForm):
    class Meta:
        model=Order
        widgets = {
            'order_date': widgets.AdminDateWidget(),
            'order_time': widgets.AdminTimeWidget(format='%H:%M')
        }