Django TimeField Model without seconds

前端 未结 7 2141
青春惊慌失措
青春惊慌失措 2021-02-08 12:48

Greetings, I am trying to implement a TimeField model which only consists of HH:MM (ie 16:46) format, I know it is possible to format a regular Python time object but I am lost

7条回答
  •  说谎
    说谎 (楼主)
    2021-02-08 13:18

    For a ModelForm, you can easily add a widget like this, to avoid the seconds being shown (just show hh:mm):

    class MyCreateForm(forms.ModelForm):
    
        class Meta:
            model = MyModel
            fields = ('time_in', 'time_out', )
            widgets = {
                'time_in': forms.TimeInput(format='%H:%M'),
                'time_out': forms.TimeInput(format='%H:%M'),
            }
    

提交回复
热议问题