更新时间:2022-01-29 22:46:22
如果您不怕添加依赖项: django-select2 具有链接选择的实现,可以使用django表单API对其进行配置。他们的文档中的示例:
If you are not afraid of adding dependencies: django-select2 has an implementation of chained selects, which can be configured using the django form API. Example from their docs:
class AddressForm(forms.Form):
country = forms.ModelChoiceField(
queryset=Country.objects.all(),
label=u"Country",
widget=ModelSelect2Widget(
model=Country,
search_fields=['name__icontains'],
)
)
city = forms.ModelChoiceField(
queryset=City.objects.all(),
label=u"City",
widget=ModelSelect2Widget(
model=City,
search_fields=['name__icontains'],
dependent_fields={'country': 'country'},
max_results=500,
)
)