我有這個過濾機制,但是,它不是很優雅。必須有更好的方式來寫這個。任何建議將不勝感激。django複合查詢集
用戶能夠從多個過濾器選擇過濾列表:
forms.py
class FilterForm(forms.Form):
def __init__(self, *args, **kwargs):
super(FilterForm, self).__init__(*args, **kwargs)
self.fields['group'].widget.attrs["onchange"] = mark_safe('this.form.submit();')
self.fields['location'].widget.attrs["onchange"] = mark_safe('this.form.submit();')
self.fields['host'].widget.attrs["onchange"] = mark_safe('this.form.submit();')
self.fields['exchange'].widget.attrs["onchange"] = mark_safe('this.form.submit();')
group = forms.ModelChoiceField(queryset=Group.objects.all().order_by('name'),)
location = forms.ModelChoiceField(queryset=Location.objects.all().order_by('name'),)
host = forms.ModelChoiceField(queryset=Host.objects.all().order_by('name'),)
exchange = forms.ModelChoiceField(queryset=Exchange.objects.all().order_by('name'),)
views.py
initial = {}
#check for filtering
if 'group' in request.POST:
if request.POST['group']:
initial['group'] = request.POST['group']
obj = Group.objects.get(pk=request.POST['group'])
selectForm.fields['job'].queryset = selectForm.fields['job'].queryset.filter(group=obj)
if 'host' in request.POST:
if request.POST['host']:
initial['host'] = request.POST['host']
obj = Host.objects.get(pk=request.POST['host'])
selectForm.fields['job'].queryset = selectForm.fields['job'].queryset.filter(host=obj)
if 'location' in request.POST:
if request.POST['location']:
initial['location'] = request.POST['location']
obj = Location.objects.get(pk=request.POST['location'])
selectForm.fields['job'].queryset = selectForm.fields['job'].queryset.filter(colo=obj)
if 'exchange' in request.POST:
if request.POST['exchange']:
initial['exchange'] = request.POST['exchange']
obj = Exchange.objects.get(pk=request.POST['exchange'])
selectForm.fields['job'].queryset = selectForm.fields['job'].queryset.filter(exchange=obj)
filterForm.initial = initial
我不認爲它適用於位置字段。該過濾器需要過濾器(colo = obj)而不是位置= obj – 2011-05-20 15:17:15
大壩沒有發現「colo」不同。 – JamesO 2011-05-20 15:21:56
這樣一個奇怪的名字爲一個位置fkey字段 – 2011-05-20 15:22:30