Background-color of AutocompleteSelect field not working

136 Views Asked by At

I want to set the background-color of a AutocompleteSelect field. When the page loads, the background-color is applied, but will be overwritten immediately (When loading the page, I see the background-color red, but it immediately changes again). The style="width:110px" is applied correctly, but not the background-color.

Code of the form:

class ProjectLineAdminForm(forms.ModelForm):
work_type = forms.ModelChoiceField(
        WorkType.objects.all(),
        widget=AutocompleteSelect(ProjectLine._meta.get_field('work_type'), admin.site),
)

    def __init__(self, *args, **kwargs):
         super(ProjectLineAdminForm, self).__init__(*args, **kwargs)
 
         # Set background colors of worktype
         if self.instance.work_type:
             if self.instance.work_type.code == 'JOHN':
                 self.fields['work_type'].widget.attrs.update({'style': 'width:110px; background-color: red'})

Code of the Inline class:

class ProjectLineInline(admin.TabularInline):
    model = ProjectLine
    fields = ("work_type",)
    form = ProjectLineAdminForm

I don't have this problem with other fields.

0

There are 0 best solutions below