Anish Nepal
Anish Nepal

Reputation: 11

getting multiple user objects using filter

Lets say, I have a User object and I need to get a queryset with username 'foo' and 'bar'. How do i use the filter to achieve this since filter doesnot take two same kwargs.

My approach:

#obviously shows error
users=User.objects.filter(username='foo', username='bar')

What is the best way to do this?

Upvotes: 1

Views: 49

Answers (3)

django-renjith
django-renjith

Reputation: 4100

users = User.objects.filter(username__in=['foo', 'bar'])

Upvotes: 0

falsetru
falsetru

Reputation: 369444

You can use <fieldname>__in:

users = User.objects.filter(username__in=['foo', 'bar'])

Upvotes: 1

catavaran
catavaran

Reputation: 45595

users = User.objects.filter(username__in=['foo', 'bar'])

Read the documentation

Upvotes: 1

Related Questions