Search code examples
djangotimefield

Formatting TimeField


I'm overriding my model admin form in order to change the format of the displayed time of a TimeField field:

class myTimeForm(forms.ModelForm):
    start_time = forms.TimeField(widget=forms.TimeInput(format='%H:%M'))
    class Meta:
        model = MyModel

class MyModelAdmin(admin.ModelAdmin):
    form = myTimeForm

Now, the now and the clock button I had with the default form have disappeared (in the screenshot below Start time is with the overridden widget, End time with the default one.

enter image description here

What did I miss?


Solution

  • The Django admin uses the AdminTimeWidget instead of forms.TimeInput. Try changing your code to the following:

    from django.contrib.admin.widgets import AdminTimeWidget
    
    class myTimeForm(forms.ModelForm):
        start_time = forms.TimeField(widget=AdminTimeWidget(format='%H:%M'))
        class Meta:
            model = MyModel