I implement read-only fields by using widgets that render html that
does not include any <input>.   For example, if I have a field called
hobby in a Customer model, in my form I have something like this:

self.fields["hobby"] = CharField(widget=DisplayHobbyField(customer),
required=False)

DisplayHobbyField::__init__() saves customer in self.customer, and
then its DisplayHobbyField::render renders the value of
customer.hobby.

In order for this to work, you need to *not* put hobby into the form's
Meta.fields list (if this is a modelForm).

If this is a model form, I'm not sure if you would be able to use the
same form for your admin and your non-admin, due to the fact that in
the admin form, you would need hobby to be in Meta.fields, while in
the non-admin form you would need hobby to not be in Meta.fields.
Since Meta.fields is a static field, it seems to me that there is no
way for some versions to the form to have it set one way, and others
have it set another way.  But I am not sure about this.

The way I get around this is to always put hobby into Meta.fields  In
the case where it's a read only field, I send it as a hidden field so
that it goes out in the GET and then gets posted in the POST.  Then I
have a separate read only field that does rendering of the read-only
field  In other words, have

class Meta:
    fields = ("hobby",)

if user is admin:
   self.fields["hobby"] = CharField()
else:
    self.fields["displayHobby"] =
CharField(widget=DisplayHobbyField(customer), required=False)
    self.fields['hobby"} = CharField(widget=HiddenInput())

I have a number of different ways of rendering forms for the same
model, and after much trial and error, I found that my code became
most maintainable when all forms always send the same set of model
fields in their get and post data.  In the case where the field needs
to be read-only, I send the data in hidden fields (by using
specialized widgets).  This allows me to reuse a lot of code (such as
my save methods) without having a lot of special case code that
understand which field are in the model.

I hope this makes sense.  I've seen a lot of people asking about read
only fields and I've found that this mechanism woks really well for
me.  The ability to use special widgets to make your forms specialized
for various purposes is very powerful.


On Mar 20, 8:05 am, Peter Reimer <pet.rei...@googlemail.com> wrote:
> Hi Folks,
>
> I'm looking for a way to get the following done:
> Depending on different things (user, groups, rights, etc.) I want to
> show a form with fields that can either be changed or not. For
> example, a form with two fields for name and hobby, an admin can
> change both fields, a normal user can only change hobby.
>
> One solution that came to mind: Create a form for both groups and
> exclude in the user's form the fields, he mustn't change. But I want
> two things:
> 1. Show the read-only values at the position where the admin sees the
> writeable fields because a user can be an admin (at another place) and
> it would be confusing to have different positions here and there.
> 2. Have something reusable: E.g. form = MySpecialForm(deactivated =
> [name]).
>
> I checked to docu and googled around, not only some minutes... and
> found nothing.
> Has somebody here a hint for me? I don't want a solution, I'm thankful
> for some little hints.
>
> Thanks & have a nice weekend
> Peter

-- 
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