Ticket #3050: values-5383.diff
File values-5383.diff, 3.4 KB (added by , 17 years ago) |
---|
-
django/db/models/query.py
554 554 class ValuesQuerySet(QuerySet): 555 555 def __init__(self, *args, **kwargs): 556 556 super(ValuesQuerySet, self).__init__(*args, **kwargs) 557 # select_related and select aren't supported in values().557 # select_related isn't supported in values(). 558 558 self._select_related = False 559 self._select = {}560 559 561 560 def iterator(self): 562 561 try: … … 566 565 567 566 # self._fields is a list of field names to fetch. 568 567 if self._fields: 569 columns = [self.model._meta.get_field(f, many_to_many=False).column for f in self._fields] 568 #columns = [self.model._meta.get_field(f, many_to_many=False).column for f in self._fields] 569 if not self._select: 570 columns = [self.model._meta.get_field(f, many_to_many=False).column for f in self._fields] 571 else: 572 columns = [] 573 for f in self._fields: 574 if f in [field.name for field in self.model._meta.fields]: 575 columns.append( self.model._meta.get_field(f, many_to_many=False).column ) 576 elif not self._select.has_key( f ): 577 raise FieldDoesNotExist, '%s has no field named %r' % ( self.model._meta.object_name, f ) 578 570 579 field_names = self._fields 571 580 else: # Default to all fields. 572 581 columns = [f.column for f in self.model._meta.fields] 573 582 field_names = [f.attname for f in self.model._meta.fields] 574 583 575 584 select = ['%s.%s' % (backend.quote_name(self.model._meta.db_table), backend.quote_name(c)) for c in columns] 585 586 # Add any additional SELECTs. 587 if self._select: 588 select.extend(['(%s) AS %s' % (quote_only_if_word(s[1]), backend.quote_name(s[0])) for s in self._select.items()]) 589 576 590 cursor = connection.cursor() 577 591 cursor.execute("SELECT " + (self._distinct and "DISTINCT " or "") + ",".join(select) + sql, params) 578 592 while 1: -
tests/modeltests/lookup/models.py
131 131 [('headline', 'Article 7'), ('id', 7)] 132 132 [('headline', 'Article 1'), ('id', 1)] 133 133 134 135 # you can use values() even on extra fields 136 >>> for d in Article.objects.extra( select={'id_plus_one' : 'id + 1'} ).values('id', 'id_plus_one'): 137 ... i = d.items() 138 ... i.sort() 139 ... i 140 [('id', 5), ('id_plus_one', 6)] 141 [('id', 6), ('id_plus_one', 7)] 142 [('id', 4), ('id_plus_one', 5)] 143 [('id', 2), ('id_plus_one', 3)] 144 [('id', 3), ('id_plus_one', 4)] 145 [('id', 7), ('id_plus_one', 8)] 146 [('id', 1), ('id_plus_one', 2)] 147 148 # however, an exception FieldDoesNotExist will still be thrown 149 # if you try to access non-existent field (field that is neither on the model nor extra) 150 >>> Article.objects.extra( select={'id_plus_one' : 'id + 1'} ).values('id', 'id_plus_two') 151 Traceback (most recent call last): 152 ... 153 FieldDoesNotExist: Article has no field named 'id_plus_two' 154 134 155 # if you don't specify which fields, all are returned 135 156 >>> list(Article.objects.filter(id=5).values()) == [{'id': 5, 'headline': 'Article 5', 'pub_date': datetime(2005, 8, 1, 9, 0)}] 136 157 True