Django:使用列表上方的过滤器获取查询集(已填充和/或为空)

时间:2018-10-18 14:28:53

标签: python django django-queryset django-q

我正在开发一个模块,该模块可以通过用户定义的过滤器来显示对象。

View.py文件:

search_category = Document.objects.values_list('publication__category__name', flat=True).distinct()
kwargs['search_category'] = search_category

search_format = Document.objects.values_list('format', flat=True).distinct()
kwargs['search_format'] = search_format

search_language = Document.objects.values_list('language', flat=True).distinct()
kwargs['search_language'] = search_language

checkbox_category = self.request.GET.getlist('CategoryChoice')
checkbox_format = self.request.GET.getlist('FormatChoice')
checkbox_language = self.request.GET.getlist('LanguageChoice')
choice_title = self.request.GET.get('TitleChoice')

test_research = Document.objects\
            .filter(Q(publication__category__name__isnull=True) | Q(publication__category__name__in=checkbox_category))\
            .filter(Q(format__isnull=True) | Q(format__in=checkbox_format))\
            .filter(Q(language__isnull=True) | Q(language__in=checkbox_language))\
            .filter(Q(title__isnull=True) | Q(title__in=choice_title))

我的一种过滤器(语言)的模板文件:

<button class="btn btn-default btn-choice" type="button" data-toggle="collapse" data-target="#language"
                aria-expanded="false" aria-controls="language"><span
          class="glyphicon glyphicon-chevron-down"></span> {% trans 'Language' %}</button>
        <div class="row">
          <div class="col">
            <div class="collapse multi-collapse" id="language">
              <div class="card card-body card-choice">
                {% for language in search_language %}
                  <input type="checkbox" class="fakeRadio" name="LanguageChoice" value="{{ language }}">
                  {{ language }}<br>
                {% endfor %}
              </div>
            </div>
          </div>
        </div>

在我的Web应用程序中看起来像这样:

enter image description here

我可以选中一个或多个复选框,但也可以不选中任何复选框以符合特定条件。最后一个选项引起了问题,因为例如,如果我不检查language,则结果查询集为空,并且不应该是因为它应该考虑其他过滤器。

示例:

Object 1 : Category = 'FOOD_CONTACT', Format = 'pdf', Language = 'FR', Title = 'This is my first title'
Object 2 : Category = 'FOOD_CONTACT', Format = 'epub', Language = 'EN', Title = 'This is my second title'
Object 3 : Category = 'OTHER', Format = 'pdf', Language = 'FR', Title = 'ZZ'

现在,如果我检查这个问题:

Category filter : ['FOOD CONTACT']
Format filter : []
Language filter : []
Title filter : 
It should return : Object 1, Object 2

Category filter : ['FOOD CONTACT']
Format filter : ['pdf']
Language filter : []
Title filter : 
It should return : Object 1

Category filter : []
Format filter : ['pdf']
Language filter : []
Title filter : 'Z'
It should return : Object 3

1 个答案:

答案 0 :(得分:1)

我认为解决方案是动态地构建您的过滤器,只是不包括用户不要求的任何过滤器。

# default to all documents
test_research = Document.objects.all()

# if user entered any search criteria, add those filters
if category:
    test_research = test_research.filter(publication__category__name__in=category)

if format:
    test_research = test_research.filter(format__in=format)

if language:
    test_research = test_research.filter(language__in=language)

if title:
    test_research = test_research.filter(title__icontains=title)