Ticket #3050: values.diff
File values.diff, 3.2 KB (added by , 18 years ago) |
---|
-
django/db/models/query.py
510 510 511 511 class ValuesQuerySet(QuerySet): 512 512 def iterator(self): 513 # select_related and select aren't supported in values().513 # select_related isn't supported in values(). 514 514 self._select_related = False 515 self._select = {}516 515 517 516 # self._fields is a list of field names to fetch. 518 517 if self._fields: 519 columns = [self.model._meta.get_field(f, many_to_many=False).column for f in self._fields] 518 if not self._select: 519 columns = [self.model._meta.get_field(f, many_to_many=False).column for f in self._fields] 520 else: 521 columns = [] 522 for f in self._fields: 523 if f in [field.name for field in self.model._meta.fields]: 524 columns.append( self.model._meta.get_field(f, many_to_many=False).column ) 525 elif not self._select.has_key( f ): 526 raise FieldDoesNotExist, '%s has no field named %r' % ( self.model._meta.object_name, f ) 527 528 520 529 field_names = self._fields 521 530 else: # Default to all fields. 522 531 columns = [f.column for f in self.model._meta.fields] … … 525 534 cursor = connection.cursor() 526 535 select, sql, params = self._get_sql_clause() 527 536 select = ['%s.%s' % (backend.quote_name(self.model._meta.db_table), backend.quote_name(c)) for c in columns] 537 538 # Add any additional SELECTs. 539 if self._select: 540 select.extend(['(%s) AS %s' % (quote_only_if_word(s[1]), backend.quote_name(s[0])) for s in self._select.items()]) 541 528 542 cursor.execute("SELECT " + (self._distinct and "DISTINCT " or "") + ",".join(select) + sql, params) 529 543 while 1: 530 544 rows = cursor.fetchmany(GET_ITERATOR_CHUNK_SIZE) -
tests/modeltests/lookup/models.py
106 106 [('headline', 'Article 7'), ('id', 7)] 107 107 [('headline', 'Article 1'), ('id', 1)] 108 108 109 # you can use values() even on extra fields 110 >>> for d in Article.objects.extra( select={'id_plus_one' : 'id + 1'} ).values('id', 'id_plus_one'): 111 ... i = d.items() 112 ... i.sort() 113 ... i 114 [('id', 5), ('id_plus_one', 6)] 115 [('id', 6), ('id_plus_one', 7)] 116 [('id', 4), ('id_plus_one', 5)] 117 [('id', 2), ('id_plus_one', 3)] 118 [('id', 3), ('id_plus_one', 4)] 119 [('id', 7), ('id_plus_one', 8)] 120 [('id', 1), ('id_plus_one', 2)] 121 122 # however, an exception FieldDoesNotExist will still be thrown 123 # if you try to access non-existent field (field that is neither in model nor extra) 124 >>> Article.objects.extra( select={'id_plus_one' : 'id + 1'} ).values('id', 'id_plus_two') 125 Traceback (most recent call last): 126 ... 127 FieldDoesNotExist: Article has no field named 'id_plus_two' 128 109 129 # You can use values() with iterator() for memory savings, because iterator() 110 130 # uses database-level iteration. 111 131 >>> for d in Article.objects.values('id', 'headline').iterator():