[Fixed]-Django. Q objects dynamicaly generate

1👍

You can do

>>>[Q(**{field +'__contains': search_text}) for field in fields_name]

Now if you want to do an OR

>>>import operator
>>>reduce(operator.or_, [Q(**{field+'__contains': search_text}) for field in fields_name])

Which is the same as

Q(title__icontains=search_text) |
Q(description__icontains=search_text)|
Q(name_icontains=search_text)

Leave a comment