Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pass a list of Qs to filter for OR lookups?

How do I pass a list of Qs to filter for OR lookups? Something like:

q_list = [Q(xyz__isnull=True), Q(x__startswith='x')]?

Without a list I would do:

Model.objects.filter(Q(xyz__isnull=True) | Q(x__startswith='x'))
like image 416
bjunix Avatar asked Nov 29 '22 07:11

bjunix


1 Answers

Use python's reduce() function:

import operator
reduced_q = reduce(operator.or_, q_list)
Model.objects.filter(reduced_q)
like image 194
bjunix Avatar answered Dec 05 '22 15:12

bjunix