1

我想设置AutocompleteSelect字段的背景颜色。页面加载时,背景颜色被应用,但会立即被覆盖(加载页面时,我看到背景颜色为红色,但它立即再次更改)。正确应用,但不是background -colorstyle="width:110px"

表格代码:

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'})

内联类的代码:

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

我在其他领域没有这个问题。

4

0 回答 0