Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #3463 -- EmptyQuerySet's iterator() now returns a generator. Th…

…anks, Gary Wilson

git-svn-id: http://code.djangoproject.com/svn/django/trunk@4475 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 007f17d63e05d8422ec63eab75d6a97fd3175be9 1 parent 1aa1c46
Adrian Holovaty authored February 10, 2007
34  django/db/models/query.py
@@ -167,17 +167,16 @@ def __or__(self, other):
167 167
 
168 168
     def iterator(self):
169 169
         "Performs the SELECT database lookup of this QuerySet."
  170
+        try:
  171
+            select, sql, params = self._get_sql_clause()
  172
+        except EmptyResultSet:
  173
+            raise StopIteration
  174
+
170 175
         # self._select is a dictionary, and dictionaries' key order is
171 176
         # undefined, so we convert it to a list of tuples.
172 177
         extra_select = self._select.items()
173 178
 
174 179
         cursor = connection.cursor()
175  
-        
176  
-        try:
177  
-            select, sql, params = self._get_sql_clause()
178  
-        except EmptyResultSet:
179  
-            raise StopIteration
180  
-            
181 180
         cursor.execute("SELECT " + (self._distinct and "DISTINCT " or "") + ",".join(select) + sql, params)
182 181
         fill_cache = self._select_related
183 182
         index_end = len(self.model._meta.fields)
@@ -523,11 +522,18 @@ def _get_sql_clause(self):
523 522
         return select, " ".join(sql), params
524 523
 
525 524
 class ValuesQuerySet(QuerySet):
526  
-    def iterator(self):
  525
+    def __init__(self, *args, **kwargs):
  526
+        super(ValuesQuerySet, self).__init__(*args, **kwargs)
527 527
         # select_related and select aren't supported in values().
528 528
         self._select_related = False
529 529
         self._select = {}
530 530
 
  531
+    def iterator(self):
  532
+        try:
  533
+            select, sql, params = self._get_sql_clause()
  534
+        except EmptyResultSet:
  535
+            raise StopIteration
  536
+
531 537
         # self._fields is a list of field names to fetch.
532 538
         if self._fields:
533 539
             columns = [self.model._meta.get_field(f, many_to_many=False).column for f in self._fields]
@@ -535,15 +541,9 @@ def iterator(self):
535 541
         else: # Default to all fields.
536 542
             columns = [f.column for f in self.model._meta.fields]
537 543
             field_names = [f.attname for f in self.model._meta.fields]
538  
-
539  
-        cursor = connection.cursor()
540  
-        
541  
-        try:
542  
-            select, sql, params = self._get_sql_clause()
543  
-        except EmptyResultSet:
544  
-            raise StopIteration
545 544
         
546 545
         select = ['%s.%s' % (backend.quote_name(self.model._meta.db_table), backend.quote_name(c)) for c in columns]
  546
+        cursor = connection.cursor()
547 547
         cursor.execute("SELECT " + (self._distinct and "DISTINCT " or "") + ",".join(select) + sql, params)
548 548
         while 1:
549 549
             rows = cursor.fetchmany(GET_ITERATOR_CHUNK_SIZE)
@@ -592,9 +592,6 @@ def __init__(self, model=None):
592 592
         super(EmptyQuerySet, self).__init__(model)
593 593
         self._result_cache = []
594 594
         
595  
-    def iterator(self):
596  
-        raise StopIteration
597  
-        
598 595
     def count(self):
599 596
         return 0
600 597
         
@@ -606,6 +603,9 @@ def _clone(self, klass=None, **kwargs):
606 603
         c._result_cache = []
607 604
         return c
608 605
 
  606
+    def _get_sql_clause(self):
  607
+        raise EmptyResultSet
  608
+
609 609
 class QOperator(object):
610 610
     "Base class for QAnd and QOr"
611 611
     def __init__(self, *args):
2  tests/modeltests/lookup/models.py
@@ -198,6 +198,8 @@ def __str__(self):
198 198
 []
199 199
 >>> Article.objects.none().count()
200 200
 0
  201
+>>> [article for article in Article.objects.none().iterator()]
  202
+[]
201 203
 
202 204
 # using __in with an empty list should return an empty query set
203 205
 >>> Article.objects.filter(id__in=[])

0 notes on commit 007f17d

Please sign in to comment.
Something went wrong with that request. Please try again.