2017-02-14 46 views
0

我使用我的模型中的值填充下拉框,我的模型中有五個值,我只想在這個特定的實例中顯示三個這些值,我將如何做到這一點。如何過濾下拉列表中的值django

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