[Django]-Dynamically combine Q() – OR objects

3πŸ‘

βœ…

If you are wanting to build the query as X = Y OR W = Z it’s because of

query_kwargs["{0}__{1}".format(field, search_type)] = term

You’re adding more keys to query_kwargs with each iteration of the loop rather than re-creating the variable
Would suggest something like this

for field, search_type in self.searchable_fields:
    query_q |= Q(**{"{0}__{1}".format(field, search_type): term})
πŸ‘€justcompile

Leave a comment