simple way for QuerySet union and subtraction in django?

后端 未结 5 854
醉话见心
醉话见心 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条回答
  •  时光取名叫无心
    2021-02-07 02:32

    Subtract a QuerySet from another QuerySet using the same model.

    This works - but is probably slowly

    queryset_with_hello = Blog.objects.filter(name__icontains='hello')
    queryset_without_hello = Blog.objects.exclude(pk__in=queryset_with_hello)
    

    Read the performance considerations in django documentation:

    https://docs.djangoproject.com/en/dev/ref/models/querysets/#in

提交回复
热议问题