> I cobbled these two classes together which work very nicely for me,
> wish they could make into django.
> Drop these two classes into a util.py and import them instead of Form
> or ModelForm. When you want your form readonly instantiate it with
> "form=MyForm(record, readonly=True)"
>
> class roForm(forms.Form):
> def __init__(self, *args, **kwargs):
> self.readonly = False
> if kwargs.has_key('readonly'):
> if kwargs['readonly']:
> self.readonly = True
> kwargs.pop('readonly')
> super(roForm, self).__init__(*args, **kwargs)
> if self.readonly:
> for key in self.fields.iterkeys():
> self.fields[key].widget.attrs['disabled'] = True
>
Just a stylistic point. It's easy to second-guess someone else's code
when you have the liberty of scrutinizing it at what I sometimes
laughingly call "leisure", so I don't want this to be interpreted as
being critical, but the code might (?) be easier to read (barring typos) as:
class roForm(forms.Form):
def __init__(self, *args, **kwargs):
self.readonly = 'readonly' in kwargs
if self.readonly:
kwargs.pop('readonly')
super(roForm, self).__init__(*args, **kwargs)
if self.readonly:
for key in self.fields.iterkeys():
self.fields[key].widget.attrs['disabled'] = True
regards
Steve
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
No comments:
Post a Comment