1👍
✅
Have you tried setting the attribute in the Form’s Meta class?
I experienced an issue where Form attributes were not applied for Model Fields if set in the base class definition, but they worked correctly in the Meta class:
class CompanyNoteForm(forms.ModelForm):
class Meta:
model = models.CompanyNote
widgets = {'company': forms.widgets.Select(attrs={'readonly': True,
'disabled': True})}
Otherwise check this answer out.
Worst case scenario, make company a hidden field?
1👍
Use a ModelChoiceField
class CompanyNoteForm(forms.ModelForm):
company = forms.ModelChoiceField(queryset=models.Company.objects.all(), widget=forms.widgets.Select(attrs={'readonly': 'readonly'}))
- [Answered ]-A list model field for Models.Model
- [Answered ]-Can't create superuser with custom userprofile
0👍
I could not find this answer anywhere, that I could actually get to work. But I found a different approach. Set the field to be hidden with forms.HiddenInput() widget. Then the value you pass in from the view will be assigned but the user cannot access it.
widgets = {'field_name': forms.HiddenInput()}
I’m using ModelForm class so my syntax might be different from yours.
- [Answered ]-Change Django alphabetical ordering to custom ordering
- [Answered ]-Django and Connector/Python Integration
Source:stackexchange.com