I have a BooleanField in my form which I want to be only True. Any ideas how to easily achieve this? Is there an attribute to prevent changing of it?
Asked
Active
Viewed 1,799 times
1 Answers
8
Yes, it's possible using widget attributes.
Code:
from django import forms
class MyForm(forms.Form):
ReadonlyBoolField = forms.BooleanField(initial = True)
ReadonlyBoolField.widget.attrs['readonly'] = True
Testing:
>>>f = MyForm()
>>>str(f)
'<tr><th><label for="id_ReadonlyBoolField">Readonlyboolfield:</label></th><td><input readonly="True" type="checkbox" name="ReadonlyBoolField" id="id_ReadonlyBoolField" /></td></tr>'
Links:
-
I solved it by creating a ReadOnlyBoolField with title="[Not editable]" – Capuchin Apr 09 '14 at 10:47