Ticket #3463: iterator.diff

File iterator.diff, 3.3 KB (added by Gary Wilson <gary.wilson@…>, 18 years ago)
  • django/db/models/query.py

    === modified file 'django/db/models/query.py'
     
    167167
    168168    def iterator(self):
    169169        "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
    170175        # self._select is a dictionary, and dictionaries' key order is
    171176        # undefined, so we convert it to a list of tuples.
    172177        extra_select = self._select.items()
    173178
    174179        cursor = connection.cursor()
    175        
    176         try:
    177             select, sql, params = self._get_sql_clause()
    178         except EmptyResultSet:
    179             raise StopIteration
    180            
    181180        cursor.execute("SELECT " + (self._distinct and "DISTINCT " or "") + ",".join(select) + sql, params)
    182181        fill_cache = self._select_related
    183182        index_end = len(self.model._meta.fields)
     
    523522        return select, " ".join(sql), params
    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 = {}
    530530
     531    def iterator(self):
     532        try:
     533            select, sql, params = self._get_sql_clause()
     534        except EmptyResultSet:
     535            raise StopIteration
     536
    531537        # self._fields is a list of field names to fetch.
    532538        if self._fields:
    533539            columns = [self.model._meta.get_field(f, many_to_many=False).column for f in self._fields]
     
    535541        else: # Default to all fields.
    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:
    549549            rows = cursor.fetchmany(GET_ITERATOR_CHUNK_SIZE)
     
    592592        super(EmptyQuerySet, self).__init__(model)
    593593        self._result_cache = []
    594594       
    595     def iterator(self):
    596         raise StopIteration
    597        
    598595    def count(self):
    599596        return 0
    600597       
     
    606603        c._result_cache = []
    607604        return c
    608605
     606    def _get_sql_clause(self):
     607        raise EmptyResultSet
     608
    609609class QOperator(object):
    610610    "Base class for QAnd and QOr"
    611611    def __init__(self, *args):
  • tests/modeltests/lookup/models.py

    === modified file 'tests/modeltests/lookup/models.py'
     
    198198[]
    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
    203205>>> Article.objects.filter(id__in=[])
Back to Top