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.
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.
Set the exclude attribute of the ModelForm 's inner Meta class to a list of fields to be excluded from the form.
Instead, django-read-only uses always installed database instrumentation to inspect executed queries and only allow those which look like reads.
class Meta:
model = my_model
fields = ['name', 'title', 'date']
widgets = {
'date': DateWidget(usel10n=True, bootstrap_version=3,),
'name': forms.TextInput(attrs={'disabled': True}),
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With