Django智能选择不工作,如果我添加CSS样式



我对django的函数"django-smart-select"有一个小问题。基本上django-smart-select工作,直到我添加一个CSS类到表单字段。

下面是我的例子:

在models.py:

from django.db import models
from smart_selects.db_fields import ChainedForeignKey 
# Create your models here.
class Company(models.Model):
ID = models.AutoField(primary_key=True, unique=True)
name = models.CharField(max_length=100)
def __str__(self):
return self.name
class Subcompany(models.Model):
ID = models.AutoField(primary_key=True)
name = models.CharField(max_length=100)
ID_company = models.ForeignKey(Company, on_delete=models.CASCADE)
def __str__(self):
return self.name
class Interventions(models.Model):
ID = models.AutoField(primary_key=True)
start_date = models.DateField()
end_date = models.DateField(blank=True, null=True) 
hours = models.DecimalField(max_digits=24, decimal_places=1, blank=True, null=True)
description = models.TextField()
signature = models.CharField(max_length=100, blank=True)
ID_company = models.ForeignKey(Company, on_delete=models.CASCADE)
ID_subcompany = ChainedForeignKey(
Subcompany,
chained_field="ID_company",
chained_model_field="ID_company",
show_all=False,
auto_choose=True,
sort=True)
def __str__(self):
return  f'{self.start_date}'

so in forms.py:

from django import forms
from django.forms import DateInput, TimeInput, NumberInput, TextInput, Textarea, Select
from app1.models import Interventions
# Create your forms here.
class Forminterventions(forms.ModelForm):
class Meta:
model = Interventions
exclude = ['ID']
fields = ['start_date', 'end_date',
'hours',
'description',
'signature',
'ID_company', 'ID_subcompany']
widgets = {
'start_date': DateInput(attrs={'class': 'inputDate'}),
'end_date': DateInput(attrs={'class': 'inputDate'}),

'hours': NumberInput(attrs={'class': 'inputNumber'}),
'description': Textarea(attrs={'class': 'inputText'}),
'signature': TextInput(attrs={'class': 'inputText'}),

'ID_company': Select(attrs={'class': 'inputCombo'}),
'ID_subcompany': Select(attrs={'class': 'inputCombo'}),
}

在style.css:

.inputDate .inputNumber .inputText .inputCombo{
color: black; 
font-family: 'EB Garamond', sans-serif; 
font-size: x-large;
text-align: center;
width: 100%;
}

我不明白的是为什么如果我从forms.py中去掉这些CSS属性行,django-smart-select工作得很好:

#'ID_company': Select(attrs={'class': 'inputCombo'}),
#'ID_subcompany': Select(attrs={'class': 'inputCombo'}),

我知道也许我可以放弃修改显示"干预"的html页面。表单与CSS。但是我希望能够以某种方式修改它,我不明白我错在哪里。

对不起,我的英语不好。

感谢大家。

最后我设法解决了这个问题,用DevTools分析django智能选择组合框。在实际操作中,django-smart-selects的依赖组合框有一个预先确定的类,用于正确的操作:类="chained-fk">

所以,如果你不使用这个类,或者强制更改它的名字(就像我做的那样),django-smart-selects正确地不起作用。为了让django smart-select正常工作,我从Forms.py中删除了:
'ID_subcompany': Select(attrs={'class': 'inputCombo'}),

然而,我设法用一些JavaScript代码改变了组合框的CSS属性。

谢谢大家。

最新更新