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
I have this filtering mechanism that works, however, it is not very elegant. There has to be a better way to write this. Any recommendations would be greatly appreciated.
The user is able to select from multiple filters to filter the list:
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你可以尝试一些类似的东西(未经测试)
you could try something along the lines of (untested)