candito123
candito123

Reputation: 65

Queryset got bigger after exclusion

This is my shell copy

 In [18]: Category.objects.all()                                                 
Out[18]: <QuerySet [<Category: cate uno>, <Category: cate dos>, <Category: cate tres>, <Category: cate cuatro>, <Category: cate cinco>, <Category: cate seis>, <Category: cate siete>, <Category: cate ocho>, <Category: cate ocho>, <Category: cate nueve>, <Category: cate diez>]>

In [19]: catesFat = Category.objects.filter(project__isnull = False)            

In [20]: catesFat                                                               
Out[20]: <QuerySet [<Category: cate uno>, <Category: cate tres>, <Category: cate cuatro>, <Category: cate uno>, <Category: cate diez>, <Category: cate seis>, <Category: cate dos>, <Category: cate uno>, <Category: cate seis>, <Category: cate siete>, <Category: cate ocho>, <Category: cate dos>, <Category: cate cinco>]>

In [21]:    

It seems fine when I use it in template, but why does it show the redundant queries so it got bigger in shell?

Upvotes: 1

Views: 24

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476614

It does not make redundant queries. It uses a JOIN, and thus returns all records in the JOIN. You can only return distinct Category objects with .distinct() [Django-doc]:

catesFat = Category.objects.filter(project__isnull=False).distinct()

Upvotes: 1

Related Questions