2

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?

Capuchin
  • 3,465
  • 6
  • 28
  • 40

1 Answers1

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:

Community
  • 1
  • 1
Dunno
  • 3,632
  • 3
  • 28
  • 43