Renamed to has_data()
This commit is contained in:
parent
c744104a18
commit
819ff2a902
3
AUTHORS
3
AUTHORS
@ -189,4 +189,5 @@ that much better:
|
|||||||
* Tom (https://github.com/tomprimozic)
|
* Tom (https://github.com/tomprimozic)
|
||||||
* j0hnsmith (https://github.com/j0hnsmith)
|
* j0hnsmith (https://github.com/j0hnsmith)
|
||||||
* Damien Churchill (https://github.com/damoxc)
|
* Damien Churchill (https://github.com/damoxc)
|
||||||
* Jonathan Simon Prates (https://github.com/jonathansp)
|
* Jonathan Simon Prates (https://github.com/jonathansp)
|
||||||
|
* Thiago Papageorgiou (https://github.com/tmpapageorgiou)
|
@ -154,22 +154,22 @@ class BaseQuerySet(object):
|
|||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
def _bool(self):
|
def has_data(self):
|
||||||
""" Avoid to open all records in an if stmt """
|
""" Retrieves whether cursor has any data. """
|
||||||
|
|
||||||
queryset = self.clone()
|
queryset = self.clone()
|
||||||
queryset._ordering = []
|
queryset._ordering = []
|
||||||
return False if queryset.first() is None else True
|
return False if queryset.first() is None else True
|
||||||
|
|
||||||
def __nonzero__(self):
|
def __nonzero__(self):
|
||||||
""" Same behaviour in Py2 """
|
""" Avoid to open all records in an if stmt in Py2. """
|
||||||
|
|
||||||
return self._bool()
|
return self.has_data()
|
||||||
|
|
||||||
def __bool__(self):
|
def __bool__(self):
|
||||||
""" Same behaviour in Py3 """
|
""" Avoid to open all records in an if stmt in Py3. """
|
||||||
|
|
||||||
return self._bool()
|
return self.has_data()
|
||||||
|
|
||||||
# Core functions
|
# Core functions
|
||||||
|
|
||||||
|
@ -3831,11 +3831,15 @@ class QuerySetTest(unittest.TestCase):
|
|||||||
|
|
||||||
queryset = Test.objects
|
queryset = Test.objects
|
||||||
if not test:
|
if not test:
|
||||||
raise AssertionError('There is data, but cursor returned False')
|
raise AssertionError('Cursor has data and returned False')
|
||||||
|
|
||||||
queryset.next()
|
queryset.next()
|
||||||
if not queryset:
|
if not queryset:
|
||||||
raise AssertionError('There is data, cursor must return True')
|
raise AssertionError('Cursor has data and it must returns False,'
|
||||||
|
' even in the last item.')
|
||||||
|
|
||||||
|
self.assertTrue(queryset.has_data(), 'Cursor has data and '
|
||||||
|
'returned False')
|
||||||
|
|
||||||
def test_bool_performance(self):
|
def test_bool_performance(self):
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user