I want to make my option field inside the model form disabled. Is there any method to make the Python Django form field disabled?
so to perform this action, I find out the simplest way to solve this problem. Field.disabled will work perfectly in this case.
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( If the user try to change the form by editing the raw HTML using the inspect of the browser).
MyPersonalForm(ModelForm): def __init__(self, *args, **kwargs): super(MyPersonalForm, self).__init__(*args, **kwargs) self.fields['<field_to_disable>'].disabled = True
so for setting up any of the custom field disabled, you can simply do self.field[‘<custom_field>’].disabled = True
Before this method or in previous versions than django1.9 you have to manually change the state of the form to save the form fake post or raw HTML editing.