I’ve got a Django model with two custom manager methods. Each returns a different subset of the model’s objects, based on a different property of the object.
class FeatureManager(models.Manager):
def without_test_cases(self):
return self.get_query_set().annotate(num_test_cases=models.Count('testcase_set')).filter(num_test_cases=0)
def standardised(self):
return self.get_query_set().annotate(standardised=Count('documentation_set__standard')).filter(standardised__gt=0)
(Both testcase_set
and documentation_set
refer to ManyToManyField
s on other models.)
Is there any way to get a queryset, or just a list of objects, that’s the intersectiond of the querysets returned by each manager method?
You can just do something like this:
intersection = queryset1 & queryset2
To do a union just replace &
by |
In most cases you can just write (exploiting the "Set" part of QuerySet) :
intersection = Model.objects.filter(...) & Model.objects.filter(...)
This isn't very well documented, but should behave almost exactly like using AND conditions on conditions from both queries. Relevant code: https://github.com/django/django/blob/1.8c1/django/db/models/query.py#L203
As per Django 1.11, now it's available the function intersection()
>>> qs1.intersection(qs2, qs3)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With