user1632167
user1632167

Reputation: 133

In django How to query a model based on already existing queryset of same model

I want to do following in django:

assuming I have model called Account

accountset1 = Account.objects.filter(some query)

accountset2 = Account.objects.filter(some other query)

accounts = Account.objects.filter("account in either of accountset1 accountset2")

how can I do this

Upvotes: 1

Views: 173

Answers (1)

Rohan
Rohan

Reputation: 53326

You can make use of Q object and make a query in one line.

from django.db.models import Q
accounts = Account.objects.filter(Q(some query) | Q(some other query))

Upvotes: 2

Related Questions