Django

Code

Changeset 4475

Show
Ignore:
Timestamp:
02/09/07 23:38:38 (2 years ago)
Author:
adrian
Message:

Fixed #3463 -- EmptyQuerySet?'s iterator() now returns a generator. Thanks, Gary Wilson

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/db/models/query.py

    r4470 r4475  
    168168    def iterator(self): 
    169169        "Performs the SELECT database lookup of this QuerySet." 
    170         # self._select is a dictionary, and dictionaries' key order is 
    171         # undefined, so we convert it to a list of tuples. 
    172         extra_select = self._select.items() 
    173  
    174         cursor = connection.cursor() 
    175          
    176170        try: 
    177171            select, sql, params = self._get_sql_clause() 
    178172        except EmptyResultSet: 
    179173            raise StopIteration 
    180              
     174 
     175        # self._select is a dictionary, and dictionaries' key order is 
     176        # undefined, so we convert it to a list of tuples. 
     177        extra_select = self._select.items() 
     178 
     179        cursor = connection.cursor() 
    181180        cursor.execute("SELECT " + (self._distinct and "DISTINCT " or "") + ",".join(select) + sql, params) 
    182181        fill_cache = self._select_related 
     
    524523 
    525524class ValuesQuerySet(QuerySet): 
    526     def iterator(self): 
     525    def __init__(self, *args, **kwargs): 
     526        super(ValuesQuerySet, self).__init__(*args, **kwargs) 
    527527        # select_related and select aren't supported in values(). 
    528528        self._select_related = False 
    529529        self._select = {} 
     530 
     531    def iterator(self): 
     532        try: 
     533            select, sql, params = self._get_sql_clause() 
     534        except EmptyResultSet: 
     535            raise StopIteration 
    530536 
    531537        # self._fields is a list of field names to fetch. 
     
    536542            columns = [f.column for f in self.model._meta.fields] 
    537543            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 
    545544         
    546545        select = ['%s.%s' % (backend.quote_name(self.model._meta.db_table), backend.quote_name(c)) for c in columns] 
     546        cursor = connection.cursor() 
    547547        cursor.execute("SELECT " + (self._distinct and "DISTINCT " or "") + ",".join(select) + sql, params) 
    548548        while 1: 
     
    593593        self._result_cache = [] 
    594594         
    595     def iterator(self): 
    596         raise StopIteration 
    597          
    598595    def count(self): 
    599596        return 0 
     
    606603        c._result_cache = [] 
    607604        return c 
     605 
     606    def _get_sql_clause(self): 
     607        raise EmptyResultSet 
    608608 
    609609class QOperator(object): 
  • django/trunk/tests/modeltests/lookup/models.py

    r4470 r4475  
    199199>>> Article.objects.none().count() 
    2002000 
     201>>> [article for article in Article.objects.none().iterator()] 
     202[] 
    201203 
    202204# using __in with an empty list should return an empty query set