Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 391
jul Avatar asked May 12 '26 03:05

jul


1 Answers

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
like image 164
Alasdair Avatar answered May 14 '26 10:05

Alasdair



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!