- April 14, 2014
- form readonly
- 1 (after 1 ratings)
The simplest way of displaying a "details" table about any model, is to show a ModelFrom with all fields readonly or (selects) disabled.
Also, the labels are preferably translatable, not just capitalized names of the column tables in your models. So the constructor translates the field labels as well.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
from django import forms class ReadOnlyForm(forms.ModelForm): """Base class for making a form readonly.""" def __init__(self, *args, **kwargs): from django.utils.translation import ugettext as _ from django.forms.widgets import Select super(ReadOnlyForm, self).__init__(*args, **kwargs) for f in self.fields: self.fields[f].label = _(self.fields[f].label) if isinstance(self.fields[f].widget, Select): self.fields[f].widget.attrs['disabled'] = 'disabled' else: self.fields[f].widget.attrs['readonly'] = 'readonly'
More like this
- Read only form & model field by StanislavKraev 5 years, 2 months ago
- Selectively change fields, widgets or labels in forms created from models by danjak 9 years, 6 months ago
- Readonly fields on Form/Modelform by Killarny 7 years, 6 months ago
- Improved model select field for generic relationships by kratorius 7 years, 1 month ago
- Replace model select widget in admin with a readonly link to the related object by ekellner 8 years ago