Django 1.9不会过滤查询结果

时间:2016-09-29 18:55:53

标签: python django-forms filtering django-1.9

我有一个表单,其中我的一个选择输入的选项与另一个表相关。

一切都很完美,但我知道我想要排除一些选项,所以我在 forms.py 中有以下代码:

class employeesForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(employeesForm, self).__init__(*args, **kwargs)
        self.fields['num_employee'].required = False
        self.fields['id_ignition'].required = False
        self.fields['picture'].required = False
        self.fields['source'].required = False

    class Meta:
        model = Employees
        ccOptions = CostCenter.objects.all().values_list('id_costCenter', 'area')
        jcOptions = JobCode.objects.all().values_list('id_jobCode', 'gmpName')
        ntOptions = Nations.objects.all().values_list('nationality')
        esOptions = EmpSources.objects.all().values_list('source')
        from django.db.models import Q
        # in this line
        stOptions = Status.objects.exclude(Q(id_status=2) | Q(id_status=3)).values_list('status') 

        fields = [
            'num_employee',
            'fullName',
            'shortName',
            'gender',
            'birthday',
            'initday',
            'id_status',
            'nationality',
            'picture',
            'source',
        ]
        widgets = {
            'num_employee': forms.NumberInput(attrs={'class': 'form-control', 'name': 'num_employee'}),
            'fullName': forms.TextInput(attrs={'class': 'form-control', 'name': 'fullName', 'placeholder': 'Angel Rafael Ortega Vazquez'}),
            'shortName': forms.TextInput(attrs={'class': 'form-control', 'name': 'shortName', 'placeholder': 'Rafael Ortega'}),
            'gender': forms.CheckboxInput(attrs={'class': 'form-control', 'name': 'gender'}),
            'birthday': forms.DateInput(attrs={'class': 'form-control', 'name': 'birthday'}),
            'initday': forms.DateInput(attrs={'class': 'form-control', 'name': 'initday'}),
            # also here
            'id_status': forms.Select(choices=stOptions, attrs={'class': 'form-control', 'name': 'id_status'}),
            'nationality': forms.Select(choices=ntOptions, attrs={'class': 'form-control', 'name': 'nationality'}),
            'picture': forms.ClearableFileInput(attrs={'class': 'form-control', 'name': 'picture'}),
            'source': forms.Select(choices=esOptions, attrs={'class': 'form-control', 'name': 'source'}),
        }

问题在于即使使用exclude选项,我的选择仍然会渲染完整的表格。

我甚至尝试过滤一个参数,但结果是一样的。 我也删除了我的浏览器缓存只是为了放弃这种可能性,但它并没有改变。

1 个答案:

答案 0 :(得分:1)

ModelForms中的相关对象是ModelChoiceField。您需要指定queryset=...属性。

示例:

class Department(models.Model):
    name = models.CharField()
    active = models.BooleanField()


class Employee(models.Model):
    name = models.CharField()
    department = models.ForeignKey(Department)


class EmployeeForm(forms.ModelForm):
    class Meta:
        model = Employee
        exclude = []

    def __init__(self, *args, **kwargs):
        super(EmployeeForm, self).__init__(*args, **kwargs)
        self.fields['department'].queryset = Department.objects.filter(active=True)