Enable ModelForm to display as read-only
|Reported by:||Owned by:||nobody|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
It seems there is not an easy way to define a ModelForm as having all its fields "read-only", so that the form can be used for only-displaying info, for instance to a non-authenticated user.
I've seen several people looking for this feature with some nasty workarounds.
A +/- clean hack is to define a function in the child class that goes iterates over the fields and defines the disabled attribute to the corresponding widget, like:
def set_readonly( self ): for field in self.fields: self.fields[field].required = False self.fields[field].widget.attrs['disabled'] = 'disabled'
is there a better way of doing this? If not, can you consider applying this solution to the main class (ModelForm)?