Actually, what he wanted was just to override a field, and not the whole
form, but he can do it with slight modifications of your code. He would
override the form class he wants to modify, instead of creating a generic
one, and instead of iterating all the fields he would just set the relevant
one(s) read only.

- Paulo

On Wed, Aug 4, 2010 at 4:03 AM, shmengie <1st...@gmail.com> wrote:

> 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
>
>
> class roModelForm(forms.ModelForm):
>    def __init__(self, *args, **kwargs):
>        self.readonly = False
>        if kwargs.has_key('readonly'):
>            if kwargs['readonly']:
>                self.readonly = True
>            kwargs.pop('readonly')
>        super(roModelForm, self).__init__(*args, **kwargs)
>        if self.readonly:
>            for key in self.fields.iterkeys():
>                self.fields[key].widget.attrs['disabled'] = True
>
>
> On Aug 3, 2:36 pm, snipinben <benjamin.calderon1...@gmail.com> wrote:
> > I was wondering how to make a field readonly when you click on it or want
> to
> > change the record. and i have done that with the readonly_feilds method.
> but
> > that makes it to where you cant add information to that feild when you
> add a
> > new record. is there something i can use where you are not able to edit a
> > feild in the change view, but when you add new, you are able to edit the
> > field until you press save? thanks alot
> > --
> > View this message in context:
> http://old.nabble.com/how-to-make-readonly-in-change-but-editable-in-...
> > Sent from the django-users mailing list archive at Nabble.com.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To post to this group, send email to django-us...@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users+unsubscr...@googlegroups.com<django-users%2bunsubscr...@googlegroups.com>
> .
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to