1👍
✅
You can build a Q
object dynamically:
from operator import or_
from functools imoprt reduce
genres = ["Action", "Comedy", "Horror"]
q = reduce(or_, (Q(genres__icontains=g) for g in genres))
# ...
qs = qs.filter(q)
This is a shortcut for:
q = Q(genres__icontains=genres[0])
for g in genres[1:]:
q |= Q(genres__icontains=g)
Source:stackexchange.com