'Django QuerySet exists() returns True for empty set?

Something curious I found recently is that exists() evaluates to True for an empty QuerySet:

   In [1]: MyModel.objects.all()
   Out [1]: <QuerySet []>

   In [2]: MyModel.objects.all().exists()
   Out [2]: True

When used with filter(), I get the expected result:

   In [3]: MyModel.objects.filter(id=1).exists()
   Out [3]: False

This is behaving contrary to how I would expect as the empty QuerySet returned by the filter() is equivalent to MyModel.objects.all() which is empty.

Why does this happen? It seems inconsistent.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source