Reputation: 1148
Let's say in some database i have People who has this attributes:
If i wanted to get all the people that are called Stuart or Steve i could use Q objects:
qs = People.objects.filter(Q(name='Stuart') | Q(name='Steve'))
But, what if i'm receiving a list of n names? Example:
['Bob, 'John','Rachel', 'Some other names that i don't know'...]
How could i build a query with those n unknown names on the list?
Upvotes: 0
Views: 74
Reputation:
the simple solution is to use standart filter __in=
name_list = ['Bob', 'John', 'Rachel']
qs = People.objects.filter(name__in=name_list)
Upvotes: 3
Reputation: 1066
One way to do this is qs = People.objects.filter(name__in=['list of names'])
, there might be another efficient way also, I am not so sure.
Upvotes: 1