If I were to have two different QuerySets in Django, both representing a ManyToMany relation with the same model, how would I find the intersections?
A:
Merge your querysets in a list and next, create a set, you'll convert back to a list :
from itertools import chain
merged_qs = chain(queryset1, queryset2)
intersection_list = list(set(list( merged_qs )))
Pierre-Jean Coudert
2010-04-04 11:54:55
A:
- order querysets by same set of keys
- call iterator() on both querysets
- feed iterators to intersect function from this answer: http://stackoverflow.com/questions/969709/joining-a-set-of-ordered-integer-yielding-python-iterators
Yaroslav
2010-04-05 21:17:41
+1
A:
You might be able to avoid the question by using the IN operator to create a subquery: http://docs.djangoproject.com/en/dev/ref/models/querysets/#in
mlissner
2010-04-20 18:47:26