[英]Django 1.9 doesnt filter query results
I have a form where the options of one of my select input are related to another table. 我有一个表单,其中我选择的输入之一的选项与另一个表相关。
Everything works perfectly, but know i want to exclude some options so i have the following kode in forms.py : 一切正常,但是我知道我想排除一些选项,所以在form.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'}),
}
The problem is that even with the exclude option, my select stills rendering the complete table. 问题在于,即使使用了exclude选项,我的select仍然可以渲染整个表。
I even tried filtering only one parameter but the result is the same. 我什至尝试仅过滤一个参数,但结果是相同的。 I also deleted my browser cache just to discard this possibility, but it didn't changed. 我还删除了浏览器缓存,只是为了放弃这种可能性,但是它没有改变。
Related objects in ModelForms are ModelChoiceField
s. ModelForms中的相关对象是ModelChoiceField
。 You need to specify the queryset=...
attribute. 您需要指定queryset=...
属性。
Example: 例:
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.