vincentf
vincentf

Reputation: 1499

What's the best way to check if a queryset is a subset of another?

If there are two querysets q1 and q2, what's the best way to check if q1 is a subset of q2 ? Or do I have to iterate the values of them ?

q1_id = q1.values_list('id', flat=True)
q2_id = q2.values_list('id', flat=True)
all([x in q2 for x in q1])

Upvotes: 2

Views: 666

Answers (1)

A. J. Parr
A. J. Parr

Reputation: 8026

You could use set to check this:

q1_id = set(q1.values_list('id', flat=True))
q2_id = set(q2.values_list('id', flat=True))
issubset = q1_id.issubset(q2_id)

Upvotes: 3

Related Questions