如何在2个组合框django形式中显示不重复的值

时间:2016-05-04 07:44:45

标签: python django

我有django形式的2个组合框,它们都显示来自db的相同资源。示例:我在db中有一个表Country包含值:'USA','Japan','England'。现在我有2个组合框从表Country获取数据显示值,如果在combobox1(from_country)我选择'USA'然后combobox2(to_country)刚播放'Japan'和'England'再次不显示'USA'。我怎样才能做到这一点?这是我的forms.py文件

class MyModelChoiceField(ModelChoiceField):
def label_from_instance(self, obj):
    return obj.name

class MyForm(forms.ModelForm):
from_country = MyModelChoiceField(queryset=None, empty_label='---Select---',
                                   widget=forms.Select(attrs={'class': 'form-control'}),
                                   error_messages={'required': 'This field is required.'})
to_country = MyModelChoiceField(queryset=None, empty_label='---Select---',
                                 widget=forms.Select(attrs={'class': 'form-control'}),
                                 error_messages={'required': 'This field is required.'})
def __init__(self, *args, **kwargs):
    super(ExchangeRateForm, self).__init__(*args, **kwargs)
    self.fields['from_country'].queryset = Country.objects.filter(is_hidden=0)
    self.fields['to_country'].queryset = Country.objects.filter(is_hidden=0)

0 个答案:

没有答案