Ticket #6899: nonzero-queryset-2.diff

File nonzero-queryset-2.diff, 1.3 KB (added by cide@…, 7 years ago)

Fixes _QuerySet.nonzero for empty query sets (with test case). Works with ValuesQuerySets.

  • django/db/models/query.py

     
    7272                iter(self).next()
    7373            except StopIteration:
    7474                return False
    75         return True
     75            else:
     76                return True
     77        return bool(self._result_cache)
    7678
    7779    def __getitem__(self, k):
    7880        "Retrieve an item or slice from the set of results."
  • tests/modeltests/nonzero/models.py

     
     1"""
     243. Boolean evaluation of QuerySets
     3
     4This tests that QuerySet.__nonzero__ behaves consistently between caching.
     5"""
     6
     7from django.db import models
     8
     9class Empty(models.Model):
     10    pass
     11
     12__test__ = {'API_TESTS':"""
     13>>> objects = Empty.objects.all()
     14>>> bool(objects)
     15False
     16>>> bool(objects)
     17False
     18>>> Empty().save()
     19>>> bool(Empty.objects.filter(pk=1).extra(select={'a':1}).values('a'))
     20True
     21"""}
Back to Top