H C
H C

Reputation: 1200

Django Queryset filter for arguments in a list

I have a list of names:

name_list = ['John', 'Bill', 'Charlie']

I would like to filter for results where the name field matches any of these names. How can I implement this in queryset? The code I am using is:

Special_group = People.objects.filter(name=name_list(?))

Thanks.

Upvotes: 4

Views: 4653

Answers (1)

Rod Xavier
Rod Xavier

Reputation: 4043

You are looking for this query.

Special_group = People.objects.filter(name__in=name_list)

This type of query is called a field_lookup in Django. There are a lot of other operations such as iexact, contains, lte, gte and a lot more.

Upvotes: 10

Related Questions