simple way for QuerySet union and subtraction in django?

后端 未结 5 798
醉话见心
醉话见心 2021-02-07 02:15

Consider two QuerySet objects of the same class. Is there a simple way to unify them into a single QuerySet by calculating the union? Also, is there a simple way to subtract the

5条回答
  •  梦毁少年i
    2021-02-07 02:39

    Going back to django's documentation, you can:

    new_query_set = query_set_1 | query_set_2
    

    This works as a logical OR which is actually addition without duplicates. This answers the addition aspect and AFAIK does not hit the db at all!

    new_query_set = query_set_1 & query_set_2
    

    This works as a logical AND.

    Still missing how to subtract QuerySets. It's hard for me to believe this has not been dealt with elegantly by the community...

提交回复
热议问题