I created a Django ModelForm.
Here is the models.py file
class Room(models. Model):
available_from = models.TimeField()
available_till = models.TimeField()
Here is the forms.py file
class RoomForm(forms.ModelForm):
class TimeInput(forms.TimeInput):
input_type = 'time'
available_from = forms.TimeField(
widget=TimeInput(),
)
available_till = forms.TimeField(
widget=TimeInput(),
)
class Meta:
model = Room
fields = ['available_from','available_till']
I want to know how to set a default time to the form.
CodePudding user response:
You can specify a default in the model if you want to do this for all forms/admins/… that are derived from this model:
from datetime import time
class Room(models. Model):
available_from = models.TimeField(default=time(2))
available_till = models.TimeField()
or if this is specific to this form, you can use the initial=…
parameter [Django-doc]:
from datetime import time
class TimeInput(forms.TimeInput):
input_type = 'time'
class RoomForm(forms.ModelForm):
available_from = forms.TimeField(
widget=TimeInput(),
initial=time(2)
)
class Meta:
model = Room
fields = ['available_from','available_till']
widgets = {'available_till': TimeInput}