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.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.