Django

Code

Ticket #6899: nonzero-queryset.diff

File nonzero-queryset.diff, 1.1 kB (added by brodie, 10 months ago)

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

  • django/db/models/query.py

    old new  
    7272                iter(self).next() 
    7373            except StopIteration: 
    7474                return False 
    75         return True 
     75        return bool(self._result_cache) 
    7676 
    7777    def __getitem__(self, k): 
    7878        "Retrieve an item or slice from the set of results." 
  • tests/modeltests/nonzero/models.py

    old new  
     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"""}