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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With