[Fixed]-Django Q Objects โ€“ Return both True and False matches

1๐Ÿ‘

โœ…

Use kwargs:

search = {'name__icontains': search_term}
if search_public is not None:
    search.update({'public__icontains': search_public})
if search_parking is not None:
    search.update({'parking__icontains': search_parking})
if search_free_entrance is not None:
    search.update({'pree_entrance__icontains': search_free_entrance})
q_objects.append(Q(**search))

or more complicated example (your question from comment):

search_kwargs = {'name__icontains': search_term}
search_args = tuple()
if search_public is not None:
    search_args += (Q(Q(company__icontains=search_public) | Q(other__icontains=search_public)),)
q_objects.append(Q(*search_args, **search_kwargs))

Leave a comment