When using Q objects in django queryset for OR condition error occured
<p>I want to use filter with OR condition in django.
For this i want to use Q objects.
My code is this</p>

<pre><code>def product(request):
    try:

        proTitle = request.GET.get('title')
        ProDescription = request.GET.get('description')


    except:
        pass


    list0 = []
    result = Product.objects.filter(Q(title__contains=proTitle ) | Q(description__contains=ProDescription ) )

    for res in result:

        list0.append(res.project_id)
    data ={'title result':list0}
    return HttpResponse(json.dumps(data))



return HttpResponse(json.dumps(data), content_type='application/json')
</code></pre>

<p>When pass all value that is <code>proTitle,ProDescription
it working fine.
If any value is going to none it encounter a error</code>Cannot use None as a query value`
Why this error occured when i am using OR operator into my queryset</p>

<p>I am also try this </p>

<p><code>result = Project.objects.filter(title__contains=proTitle) | Project.objects.filter(description__contains=ProDescription )</code></p>

<p>but same error occured
I am unable to understand actually what is the problem</p>

<p>You can build the <code>Q</code> object in a loop, discarding all values that are none. Note that after consideration, it appears that your <code>except</code> will never be reached because <code>get</code> will simply return <code>None</code> if the Get parameter is not present. You can thus replace the <code>try</code> with an <code>if</code>. </p>

<p>The following code assumes that you simply want the unset values to be ignored in the filter.</p>

<p>(This is untested--let me know if you have any issues.)</p>

<pre><code>attributes = (('title', 'title__contains'),
              ('description', 'description__contains'),
              ('funAria', 'functional_area__contains'),
              ('femaleReq', 'female_candidate'))

query_filter = Q() # initialize

for get_param, kw_arg in attributes:
    get_value = request.GET.get(get_param)
    if get_value:
        query_filter |= Q(**{ kw_arg: get_value })

result = Product.objects.filter(query_filter)
</code></pre>

python django