Hernan
Hernan

Reputation: 1148

How can i buid a query from a list using filters?

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

Answers (2)

user8060120
user8060120

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

Sammy J
Sammy J

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

Related Questions