Fabio,

if you are using Django 1.9, you can use the newly introduced disabled 
attribute on forms.Field, see 
https://docs.djangoproject.com/ja/1.9/ref/forms/fields/#disabled

On Tuesday, March 22, 2016 at 3:36:34 PM UTC+1, Fabio Caritas Barrionuevo 
da Luz wrote:
>
>
> self.fields[name].widget.attrs['disabled'] = 'disabled'
> self.fields[name].widget.attrs['readonly']=True
>
> is not make real readonly to field, because if user can edit the html on 
> client side, and remove disabled="disabled" and readonly input atributtes
>
> to problem of readonly fields, i currently use this:
>
>
> https://github.com/luzfcb/django-simple-history/blob/wip-generic-views2/simple_history/forms.py
>
>
> I prevent it here 
> https://github.com/luzfcb/django-simple-history/blob/wip-generic-views2/simple_history/forms.py#L24
>
> Usage:
>
> class FooBar(models.Model):
>     foo = models.TextField(blank=True)
>     bar = models.TextField(blank=True)
>
> class MyFooBarForm(forms.ModelForm):
>     class Meta:
>         object = FooBar
>         fields = ('foo', 'bar')
>
> class MyReadOnlyBarForm(ReadOnlyFieldsMixin, forms.ModelForm):
>     readonly_fields = ('bar')
>     class Meta:
>         object = FooBar
>         fields = ('foo', 'bar')
>
> my_all_field_readonly_foobarForm = new_readonly_form_class(MyFooBarForm)
>
> my_foo_field_readonly_foobarForm = new_readonly_form_class(MyFooBarForm, 
> readonly_fields=('foo',))
>
>
> I do not know if this is the best approach, but it was what I got so far.
>
>
>
>
>
> On Tue, Mar 22, 2016 at 10:57 AM, Gorkem Tolan <[email protected] 
> <javascript:>> wrote:
>
>> Actually I do to set permission to each field. A field in the form can be 
>> viewable and editable, only viewable, or hidden.  So if user has a 
>> permission to see the form, but edit some fields in the form, it gets very 
>> tricky especially for validation.
>> For example I used these statements to make readonly field :
>> self.fields[name].widget.attrs['disabled'] = 'disabled'
>> self.fields[name].widget.attrs['readonly']=True
>> Then the problem rises not to be able send disabled fields in the post. 
>>
>> On Tuesday, March 22, 2016 at 9:31:43 AM UTC-4, bobhaugen wrote:
>>>
>>> Maybe you already know this, but you can do a lot of form tinkering in 
>>> __init__, like so:
>>>
>>>     def __init__(self, permissions_parameter=None, *args, **kwargs):
>>>         super(FormName, self).__init__(*args, **kwargs)
>>>         if permissions_parameter:
>>>             #do a bunch of form tinkering
>>>
>> -- 
>> You received this message because you are subscribed to the Google Groups 
>> "Django users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to [email protected] <javascript:>.
>> To post to this group, send email to [email protected] 
>> <javascript:>.
>> Visit this group at https://groups.google.com/group/django-users.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/django-users/877bb8f8-4b59-4353-a0e5-99e95f55811b%40googlegroups.com
>>  
>> <https://groups.google.com/d/msgid/django-users/877bb8f8-4b59-4353-a0e5-99e95f55811b%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>>
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>
>
> -- 
> Fábio C. Barrionuevo da Luz
> Palmas - Tocantins - Brasil - América do Sul
>
> http://pythonclub.com.br/
>
> Blog colaborativo sobre Python e tecnologias Relacionadas, mantido 
> totalmente no https://github.com/pythonclub/pythonclub.github.io .
>
> Todos são livres para publicar. É só fazer fork, escrever sua postagem e 
> mandar o pull-request. Leia mais sobre como publicar em README.md e 
> contributing.md.
> Regra básica de postagem:
> "Você" acha interessante? É útil para "você"? Pode ser utilizado com 
> Python ou é útil para quem usa Python? Está esperando o que? Publica logo, 
> que estou louco para ler...
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/0fb74827-9766-49d4-bc14-57b5e7150162%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to