Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 726
H C Avatar asked Dec 24 '22 11:12

H C


1 Answers

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.

like image 151
Rod Xavier Avatar answered Jan 12 '23 22:01

Rod Xavier