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):
|
140 | 140 | return repr(data) |
141 | 141 | |
142 | 142 | def __len__(self): |
143 | | self._fetch_all() |
144 | | return len(self._result_cache) |
| 143 | return self.count() |
145 | 144 | |
146 | 145 | def __iter__(self): |
147 | 146 | """ |
… |
… |
def __iter__(self):
|
163 | 162 | return iter(self._result_cache) |
164 | 163 | |
165 | 164 | def __nonzero__(self): |
166 | | self._fetch_all() |
167 | | return bool(self._result_cache) |
| 165 | return self.exists() |
168 | 166 | |
169 | 167 | def __getitem__(self, k): |
170 | 168 | """ |