Django:获取带有列表过滤器的查询集(已填充 and/or 为空)

Django : Get queryset with filters over list (filled and/or empty)

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

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>

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

我可以选中一个或多个复选框,但也可以 none 复选框来满足特定条件。最后一个选项有问题,因为,例如,如果我不检查 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

我认为解决方案是动态构建您的过滤器,并且不要包含用户未要求的任何过滤器。

# 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)