Re: Forms with read only fields
I implement read-only fields by using widgets that render html that does not include any . 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 Reimerwrote: > 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.
Re: Forms with read only fields
You can also do that by overriding the form's __init__ : http://stackoverflow.com/questions/324477/in-a-django-form-how-to-make-a-field-readonly-or-disabled-so-that-it-cannot-be You can pass the user to init and disable fields depending on that parameter. - Paulo On Sat, Mar 20, 2010 at 4:02 PM, Thierry Chichwrote: > Le samedi 20 mars 2010 16:05:20, Peter Reimer a écrit : > > 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. > > > > I am not sure, but for me, you should have something like > > If you want it appear only when you give rights, you could put a condition > in > the template > > > I don't know if it answer to your question or if you think at something > more > complex. > > Thierry > > 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. > > -- 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.
Re: Forms with read only fields
Le samedi 20 mars 2010 16:05:20, Peter Reimer a écrit : > 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. > I am not sure, but for me, you should have something like If you want it appear only when you give rights, you could put a condition in the template I don't know if it answer to your question or if you think at something more complex. Thierry > 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.
Forms with read only fields
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.