Read-Only Field in Django Form
In django 1.9 in a Field.disabled attribute available : https://docs.djangoproject.com/en/1.9/ref/forms/fields/#disabled
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.
otherwise
use the widget 'readonly' attribute
class PatientForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(PatientForm, self).__init__(*args, **kwargs)
self.fields['field'].widget.attrs['readonly'] = True
class Meta:
model = Patient
You can use the optional attrs
parameter when defining the Field
. To wit:
somefield = forms.CharField(
widget=forms.TextInput(attrs={'readonly':'readonly'})
)