开发者

django compound querysets

开发者 https://www.devze.com 2023-03-07 02:40 出处:网络
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.

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


initial = dict(request.POST)
# The dictionary key is the field name retrieved from the POST request
# The first list item is the model to query
# The second list item is the "jobs" field to query
dict_models = {'group': [Group, 'group']],
    'host': [Host, 'host'],
    'location': [Location, 'colo'],
    'exchange': [Exchange, 'exchange'],
    }

#check for filtering
for k, v in dict_models.items():
    if initial.get(k, None):
        obj = v[0].objects.get(pk=initial[k])
        kwargs = v[1]
        for key in kwargs.keys:
            kwargs[key] = obj
        selectForm.fields['job'].queryset = selectForm.fields['job'].queryset.filter(**kwargs)

filterForm.initial = initial


you could try something along the lines of (untested)

options = {'group':Group, 'host':Host, 'location':Location, 'exchange':Exchange}
for key, modelclass in options.items():
    value = request.POST.get(key, None)
    if value:
        initial[key] = value
        obj = modelclass.objects.get(pk=value)
        filter = {}
        filter[key] = obj
        selectForm.fields['job'].queryset = selectForm.fields['job'].queryset.filter(**filter)
0

精彩评论

暂无评论...
验证码 换一张
取 消