I have a MultipleChoiceField on a form holding car makes. I want to filter my database of cars to the makes that were checked but this causes a problem. How do I get all the Q(make=...) statements in dynamically?
How I start: ['value1', 'value2', ...]
How I want to end: Q(col='value1') | Q(col='value2') | ...
I’ve couple of other methods. I’ve tried appending querysets for each make and doing it the other way around (doing multiple excludes) but they were both really slow.
Have you tried:
Model.objects.filter(make__in=list_of_makes)?The list_of_makes is what is returned by the
MultipleChoiceFieldSee the query set reference about the
__inoperator.