[Django]-Django make ModelChoiceField non-editable

2👍

Turned out, I needed here the following code:

self.fields['field_name'].widget.attrs['disabled'] = 'disabled'

1👍

This option will remove the instance from the form fields when you submit the form.

self.fields['field_name'].widget.attrs['disabled'] = 'disabled'

Using this option you will disable de field to be edited and you can converse the init instance to be saved into the database.

algorithm = forms.ModelChoiceField(label='Algoritmo',queryset=Algorithm.objects.all(),to_field_name="name",widget=forms.TextInput(attrs={'readonly':'readonly'}))

Leave a comment