0

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?

2 Answers 2

1

Use the __in lookup:

Model.objects.filter(author__in=['dave', 'steve'])
Sign up to request clarification or add additional context in comments.

2 Comments

Excellent. Ruddra suggested that I use Q but since I already have lists of values this seems more appropriate. Thanks.
what if author__in =[queryset1, queryset2]. how to do in this case filtering a single field with multiple queryset values rather than string.plz help..
1

Use Q.

Example:

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

1 Comment

Thanks. I tested this and it worked but I chose the answer by catavaran because my values were already in lists.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.