我使用模型中的值填充下拉框,我的模型中有五个值,我只想在这个特定实例中显示其中三个值,我将如何实现这一点。
forms.py
class namesForm(forms.Form):
names = forms.ModelChoiceField(
queryset=Names.objects.order_by('name').exclude(name='Josh','Tom'),
label = "Name:",
widget = Select(attrs={'class': 'span6 small-margin-top small-margin-bottom'}),
empty_label = "Select a Name",
required=True
)
答案 0 :(得分:1)
根据您的forms.py代码,我认为这是您想要的:
class namesForm(forms.Form):
names = forms.ModelChoiceField(
queryset=Names.objects.exclude(name__in=['Josh','Tom']).order_by('name'),
label = "Name:",
widget = Select(attrs={'class': 'span6 small-margin-top small-margin-bottom'}),
empty_label = "Select a Name",
required=True
)
第一行改变了:
queryset=Names.objects.exclude(name__in=['Josh','Tom']).order_by('name'),
有关使用__in的文档:
https://docs.djangoproject.com/en/1.10/ref/models/querysets/#in