0đź‘Ť
According to your question, you want to search over multiple fields. However, following the logic you have, the first result that is found in the OR sequence is returned—without returning possible subsequent matches in the OR
sequence; remember, the OR
operator stops evaluating upon a truthy result.
In order to convert your OUTER LEFT JOIN
s to INNER JOIN
s you would need to have AND
/OR
Q
object permutations of search fields combinations (optimal?), or query them separately and do an intersection on the result (sub-optimal), or write the SQL yourself (sub-optimal).
PS: I’ve run into this issue before a writing a Datatables API wrapper for use with Django.
PS: I’d consider refactoring, and further commenting your code—specifically get_filtered_queryset
; it took a few minutes for me to wrap my head around what was going on here.