Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make a disable field with modelform after django 1.9 [duplicate]

I'd like to create disabled field with ModelForm on django 1.11.

I read django has "disabled field options" since 1.9. However, I can not understand how do I define disabled field with ModelForm.

Could you tell me how to create disabled field with ModelForm please ?

Here is my models.py, forms.py and views.py


class my_model(models.Model)

    name = models.CharField(max_length=10,)
    title = models.CharField(max_length=10,)
    date = models.DateField(default=date.today,)

    def __str__(self):
        return u'%s' % (self.name)

class my_modelform(ModelForm):

    class Meta:
        model = my_model
        fields = ['name', 'title', 'date']
        widgets = {
            'date': DateWidget(usel10n=True, bootstrap_version=3,),
        }
        disabled = [ 'name' ]

class my_UpdateView(UpdateView):
    model = my_model
    form_class = my_modelform
    template_name = "update_form.html"
    success_url = "success.html"    

Although, I changed the "disabled = {'name' : True} instead of [ 'name' ], it doesn't work.

like image 307
Akira Inamori Avatar asked Mar 13 '17 18:03

Akira Inamori


People also ask

How do I disable a field in Django?

The disabled boolean argument, when set to True , disables a form field using the disabled HTML attribute so that it won't be editable by users. Even if a user tampers with the field's value submitted to the server, it will be ignored in favor of the value from the form's initial data.

How do you exclude a specific field from a ModelForm?

Set the exclude attribute of the ModelForm 's inner Meta class to a list of fields to be excluded from the form.

What is read only in Django?

Instead, django-read-only uses always installed database instrumentation to inspect executed queries and only allow those which look like reads.


1 Answers

class Meta:
    model = my_model
    fields = ['name', 'title', 'date']
    widgets = {
        'date': DateWidget(usel10n=True, bootstrap_version=3,),
        'name': forms.TextInput(attrs={'disabled': True}),
    }
like image 175
nik_m Avatar answered Nov 15 '22 14:11

nik_m