我正在使用django_select2“ ModelSelect2Widget”并获得html形式的“未找到结果”。怎么了?
models.py
class Department(Catalog):
name = models.CharField(max_length=50, unique=True)
class Person(Catalog):
surname = models.CharField(max_length=50)
name = models.CharField(max_length=50)
department = models.ForeignKey(Department, on_delete=models.PROTECT)
forms.py
class MyWidget(ModelSelect2Widget):
model = Department
search_fields = ['name__icontains', ]
class PersonForm(ModelForm):
class Meta:
model = Person
fields = ['surname', 'name', 'department']
widgets = {'department': MyWidget}
答案 0 :(得分:0)
在表单代码中进行以下更改将在表单PersonForm中工作,并且完全删除字段:
widgets={
'surname':forms.TextInput(attrs={'class':'form-control'}),
'name':forms.TextInput(attrs={'class':'form-control'}),
'department':MyWidget(attrs={'class':'form-control'}),
}
完整的PersonForm表单应类似于:
class PersonForm(ModelForm):
class Meta:
model = Person
exclude = () # If you have any column like datetime add that into `exclude`
widgets={
'surname':forms.TextInput(attrs={'class':'form-control'}),
'name':forms.TextInput(attrs={'class':'form-control'}),
'department':MyWidget(attrs={'class':'form-control'}),
}
答案 1 :(得分:0)
这是我的错误。我已经安装了“ SELECT2”,例如“ django-select2”。我通过删除其链接禁用了“ SELECT2”,但保留了“ django-select2”链接。现在MyWidget可以正常工作。