readonly field django forms

0

How to make a form field read only (readonly) in django if I have a select in the form type ModelChoiceField , and in the Model it is equivalent to an attribute type ForeignKey been dealing with this but it does not work:

class CostoForm (forms.ModelForm):

"""
Edicion de Costos con formset.
"""
class Meta:
    model = Costo
    exclude = ('user', )

def __init__(self, *args, **kwargs):
    super(CostoForm, self).__init__(*args, **kwargs)
    self.helper = FormHelper()
    self.helper.form_tag = False
    self.helper.form_action = ''
    self.helper.form_class = 'form-inline'
    self.helper.template = 'crispy_template/table_inline_formset.html'
    self.helper.layout = Layout(
        Field('DELETE', css_class='btn btn-primary')
    )
    self.helper.add_input(Submit('submit', _('Aceptar')))
    self.fields['destination'].widget.attrs['readonly'] = True
    
asked by NEFEGAGO 17.09.2018 в 18:02
source

3 answers

0

What you should use is Disabled that will make the user unable to select the field: it will be as follows:

self.fields['destination'].widget.attrs['disabled'] = 'disabled'

Here it will be necessary to see that when the user sends the form to save that field it has a value but as not specify whether the user saved or not, the above I think it solves your question

    
answered by 17.09.2018 в 18:29
0

a select already in itself is read only since you can not enter text only shows it and although you could put it "read only" this does not mean that not all data is loaded.

I suggest you create a widget of type TextInput to show this value

class CostoForm(forms.ModelForm):
    destination2 = forms.CharField(widget=forms.TextInput(attr={'readonly': True}), max_length= 100)
    class Meta:
        model = Costo
        exclude = ('user', 'destination')

    def __init__(self, *args, **kwargs):
        super(CostoForm, self).__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.form_tag = False
        self.helper.form_action = ''
        self.helper.form_class = 'form-inline'
        self.helper.template = 'crispy_template/table_inline_formset.html'
        self.helper.layout = Layout(
            Field('DELETE', css_class='btn btn-primary')
        )
        self.helper.add_input(Submit('submit', _('Aceptar')))

to add the related value you have to do it from View

class CostoView(FormView):

    model = Costo
    form_class = CostoForm
    .....
    def get_initial(self):
        initial = super(CostoView, self).get_initial()
        if self.object.destination:
             initial['destination2'] = self.object.destination.name

    ...... 
    
answered by 19.09.2018 в 09:49
0

With this code below I am checking if the user is from the Personal group to show or not fields. It may help you.

from django.contrib import admin
from .models import TuModelo    


class TuModeloAdmin(admin.ModelAdmin):
readonly_fields = ('created', 'updated')
def get_readonly_fields(self, request, obj=None):
    if request.user.groups.filter(name="Personal").exists():
        return ('created', 'updated', 'key','name')
    else:
        return ('created', 'updated')

admin.site.register(TuModelo, TuModeloAdmin)
    
answered by 23.09.2018 в 22:20