django_select2 小部件 - "No results found"
django_select2 widget - "No results found"
我正在使用 django_select2 "ModelSelect2Widget" 并使用 html-form "No results found"。怎么了?
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}
表单代码中的以下更改将适用于表单 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'}),
}
这是我的错误。除了 "django-select2",我已经安装了 "SELECT2"。我通过删除其链接禁用了 "SELECT2",但保留了 "django-select2" 链接。现在 MyWidget 工作正常。
我正在使用 django_select2 "ModelSelect2Widget" 并使用 html-form "No results found"。怎么了?
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}
表单代码中的以下更改将适用于表单 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'}),
}
这是我的错误。除了 "django-select2",我已经安装了 "SELECT2"。我通过删除其链接禁用了 "SELECT2",但保留了 "django-select2" 链接。现在 MyWidget 工作正常。