开发者

Django Querying Question

开发者 https://www.devze.com 2022-12-25 19:56 出处:网络
If I were to have two different QuerySets in Django, both representing a Man开发者_JAVA百科yToMany relation with the same model, how would I find the intersections?You might be able to avoid the quest

If I were to have two different QuerySets in Django, both representing a Man开发者_JAVA百科yToMany relation with the same model, how would I find the intersections?


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


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 )))


  1. order querysets by same set of keys
  2. call iterator() on both querysets
  3. feed iterators to intersect function from this answer: Joining a set of ordered-integer yielding Python iterators
0

精彩评论

暂无评论...
验证码 换一张
取 消