title: How to mix AND and OR operators in Django query?
body: <p>I a Django beginner, I figured out how to dynamically create a model query but I'm stuck at some point when I'm tring to mix <code>AND</code> and <code>OR</code> operators:</p>
  
  <p>I have a list of fields (contact_name, company, position, country), these fields might contain more than one keyword (separated by comma ',')</p>
  
  <p>So if I search for these company names "imtech, ABC, FooBar" I get the right result with the code I have below, because it always uses the <code>|</code> (OR) operator.</p>
  
  <p>But what if I want to restrict the search for entries with company field as "imtech, ABC, FooBar" AND position field as "Account Manager" only?</p>
  
  <p>I know I have to put the <code>&amp;</code> operator at the end of each loop of fields but I haven't succeded doing so, so here is my code, please help me!!!</p>
  
  <pre><code>search_fields = {'contact_name', 'company', 'position', 'country'}
  search_fields_values = {}
  qs_params = None
  
  for field in search_fields:
      search_fields_values[field] = self.request.GET.get(field, None)
      if search_fields_values[field]:
          search_fields_values[field] = search_fields_values[field].split(',')
  
          for part in search_fields_values[field]:
              q = Q(**{field: part})
              qs_params = qs_params | q if qs_params else q
  
  qs = qs.filter(qs_params)
  </code></pre>
  
  <p>Thanks a million!</p>