Try something like this, assuming that your date
field is forms.DateField
and that you want to use TextInput
widget:
class TestForm(forms.ModelForm):
date = forms.DateField(widget=forms.TextInput, disabled=True)
class Meta:
model = Test
fields = ['date']
This will override the default field definition which is created from your Test
model definition.
The disabled boolean argument, when set to True, disables a form field using the disabled HTML attribute so that it won’t be editable by users. Even if a user tampers with the field’s value submitted to the server, it will be ignored in favor of the value from the form’s initial data.
Read about readonly vs disabled HTML input attributes.
The key note to take out from the above SO post is:
A readonly
element is just not editable, but gets sent when the according form
submits. a disabled
element isn't editable and isn't sent on submit.
From above quote, setting disabled=True
is enough, so you dont need to set readonly
attribute on your widget.