Ticket #23662: Fix#23662.patch

File Fix#23662.patch, 1.1 KB (added by Alexey Smishlayev, 10 years ago)
  • django/db/models/query.py

    From b3825a1f77e022c0a7b7d79edceac927996cbc64 Mon Sep 17 00:00:00 2001
    From: Alexey Smishlayev <smishlayev@gmail.com>
    Date: Thu, 16 Oct 2014 11:07:32 +0300
    Subject: [PATCH] Make __nonzero__(), __len__() lazy
    
    QuerySet.__nonzero__(), QuerySet.__len__() caused unexpected behaviour by unnecessary queryset evaluation.
    Methods rewritten using proper lazy methods of QuerySet.
    ---
     django/db/models/query.py | 6 ++----
     1 file changed, 2 insertions(+), 4 deletions(-)
    
    diff --git a/django/db/models/query.py b/django/db/models/query.py
    index 6dbca83..b8d2023 100644
    a b def __repr__(self):  
    140140        return repr(data)
    141141
    142142    def __len__(self):
    143         self._fetch_all()
    144         return len(self._result_cache)
     143        return self.count()
    145144
    146145    def __iter__(self):
    147146        """
    def __iter__(self):  
    163162        return iter(self._result_cache)
    164163
    165164    def __nonzero__(self):
    166         self._fetch_all()
    167         return bool(self._result_cache)
     165        return self.exists()
    168166
    169167    def __getitem__(self, k):
    170168        """
Back to Top