Django / GET表格/ Q过滤

时间:2014-05-03 20:35:29

标签: django django-forms django-q

我正在尝试为我的结果创建过滤器,该过滤器会从html表单中获取多个值。 这个当前的设置给了我这样的网址/?language = French& language = German但是结果不会显示法语和德语记录而只显示法语。此外,添加新的过滤条件,例如" level" /?language = French& level =初学者也不起作用。

请有人帮忙并指出我正确的方向吗?

感谢 sikor

我的表格如下:

RESOURCES_LANGUAGE =        (('English', 'English'),
                            ('Spanish', 'Spanish'),
                            ('French', 'French'),
                            ('German', 'German'))

RESOURCES_LEVEL =           (('Beginner', 'Beginner'),
                            ('Intermediate', 'Intermediate'),
                            ('Advanced', 'Advanced'))

SORTBY =                    (('likes', 'Likes'),
                            ('name', 'Name'),
                            ('latest', 'Latest'))

class FiltersAndSortingForm(forms.Form): 
    language = forms.MultipleChoiceField(required=False, label='Language', widget=forms.CheckboxSelectMultiple, choices=RESOURCES_LANGUAGE)
    level = forms.MultipleChoiceField(required=False, label='Level', widget=forms.CheckboxSelectMultiple, choices=RESOURCES_LEVEL)
    provider = forms.ModelMultipleChoiceField(queryset=Provider.objects.all().order_by('name'), label='Provider', required=False,)
    sortby = forms.MultipleChoiceField(required=False, label='Sort by', widget=forms.CheckboxSelectMultiple, choices=SORTBY)

我的观点:

def resources(request):
    if request.GET:
        language = request.GET.get('language', '')
        level = request.GET.get('level', '')
        provider = request.GET.get('provider', '')  

        sortby = request.GET.get('sortby', '')
        if sortby == 'name':
            orderby = 'name'
        elif sortby == 'latest':
            orderby = '-dt_added'
        elif sortby == 'likes':
            orderby = '-facebook_likes'
        else:
            orderby = '-facebook_likes'    

        qset = (
            Q(language=language) 
#            & 
#            Q(level=level) 
            )

        resources = Resource.objects.filter(inactive=0).filter(qset).order_by(orderby)
        form = FiltersAndSortingForm()
    else:
        form = FiltersAndSortingForm()
        resources = Resource.objects.filter(inactive=0).order_by('-facebook_likes')

2 个答案:

答案 0 :(得分:1)

好的,最后看完这个帖子后django dynamically filtering with q objects我得到了这样的工作。也许这不是最干净的方式,但似乎正在做这项工作。除非有人能提出更好的解决方案吗?

感谢 -s

def resources(request):
    if request.GET:
        type = request.GET.getlist('type', '')
        language = request.GET.getlist('language', '')
        level = request.GET.getlist('level', '')
        provider = request.GET.getlist('provider', '')  

        sortby = request.GET.get('sortby', '')
        if sortby == 'name':
            orderby = 'name'
        elif sortby == 'latest':
            orderby = '-dt_added'
        elif sortby == 'likes':
            orderby = '-facebook_likes'
        else:
            orderby = '-facebook_likes'  

        qset_type = Q() # Create an empty Q object to start with
        for x in type:
            qset_type |= Q(provider__tags__name=x) # 'or' the Q objects together

        qset_language = Q() 
        for x in language:
            qset_language |= Q(language=x) 

        qset_level = Q() 
        for x in level:
            qset_level |= Q(level=x) 

        qset_provider = Q() 
        for x in provider:
            qset_provider |= Q(provider=x)    

        qset = qset_language & qset_level & qset_type & qset_provider

        resources = Resource.objects.filter(inactive=0).filter(qset).order_by(orderby)
        form = FiltersAndSortingForm()
    else:
        form = FiltersAndSortingForm()
        resources = Resource.objects.filter(inactive=0).order_by('-facebook_likes')

答案 1 :(得分:0)

我没有看到在qset中添加其他标准,这就是为什么过滤不适用于这些参数的原因。另外,我建议您使用字典作为搜索条件,然后将其解压缩。请查看此问题以获取详细说明Django: How do I use a string as the keyword in a Q() statement?。如果您需要任何澄清,请告诉我。