Thereissoupinmyfly
Thereissoupinmyfly

Reputation: 244

generating a queryset by filtering for multiple values

I'm trying to create a view that returns a queryset based on multiple values.

The pseudo code logic of what I'm trying to do is below.

Model.objects.filter(author = dave or author = steve)

can this be done in a single query? If not then what is the most efficient way of doing this?

Upvotes: 0

Views: 211

Answers (2)

ruddra
ruddra

Reputation: 51988

Use Q.

Example:

Model.objects.filter(Q(author='Dave') | Q(author='Steve'))

Upvotes: 1

catavaran
catavaran

Reputation: 45575

Use the __in lookup:

Model.objects.filter(author__in=['dave', 'steve'])

Upvotes: 1

Related Questions