Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In django, how can I filter or exclude multiple things?

If I have this queryset:

player = basketball.objects.all()

How can I do a filter where I ask for multiple people? For example: I only want players who's names are "mike" or "charles" to show up. This doesn't seem to work for me:

player.filter(name = 'mike' , 'charles')

Does anyone know the best way to go about this?

like image 955
user3806832 Avatar asked Jan 20 '15 00:01

user3806832


2 Answers

You can use __in:

player.filter(name__in=['mike', 'charles'])
like image 66
alecxe Avatar answered Nov 14 '22 23:11

alecxe


Alongside the use of __in, you can also chain multiple filters using the Q object, so objects.filter(q) where q = Q(Q(name="mike")|Q(name="phil")).

Definitely use __in though in this case.

like image 30
TankorSmash Avatar answered Nov 14 '22 23:11

TankorSmash