title: How to apply multiple filters in a Django query without knowing if some filters are null or not
body: <p>Let's say we have the following filters:</p>
  <pre><code>filter1 = request.GET.get(&quot;blabla&quot;)
  filter2 = request.GET.get(&quot;blabla&quot;)
  filter3 = request.GET.get(&quot;blabla&quot;)
  filter4 = request.GET.get(&quot;blabla&quot;)
  filter5 = request.GET.get(&quot;blabla&quot;)
  filter6 = request.GET.get(&quot;blabla&quot;)
  </code></pre>
  <p>Let's say we have the following query:</p>
  <pre><code>user_projects = Project.objects.filter(element1=filter1, element2=filter2, element3=filter3, element4=filter4, element5=filter5, element6=filter6).distinct().values(&quot;id&quot;, &quot;name&quot;, &quot;customer_id&quot;, &quot;dev_status&quot;, &quot;manager_id&quot;, &quot;total_billable&quot;)
  </code></pre>
  <p>Some of the filters might be of type <code>None</code> (depends on the request)</p>
  <p>I would like to create a dynamic query that based on whether the filter exists or not, executes the query only with the non-null filters.</p>
  <p>Let's say for example filters 1, 2, and 3 have values, but filters 4, 5, and 6 are empty/null/None.</p>
  <p>Expected query:</p>
  <pre><code>user_projects = Project.objects.filter(element1=filter1, element2=filter2, element3=filter3).distinct().values(&quot;id&quot;, &quot;name&quot;, &quot;customer_id&quot;, &quot;dev_status&quot;, &quot;manager_id&quot;, &quot;total_billable&quot;)
  </code></pre>
  <p>What I've tried:</p>
  <pre><code>if filter1 is None or filter1.strip() == &quot;&quot;:
          filter1 = &quot;&quot;
      elif is_valid_queryparam(filter1):
          user_projects = Project.objects.filter(element1=filter1).distinct().values(&quot;id&quot;, &quot;name&quot;, &quot;customer_id&quot;, &quot;dev_status&quot;, &quot;manager_id&quot;, &quot;total_billable&quot;)
  </code></pre>
  <p>And it works, but only for one filter, if I want that for <code>6 or N</code> filters, it's extremely inefficient to execute multiple queries.</p>
  <p>Is it possible to achieve the desired result with a simple query?</p>