Ticket #6811: qs_value_expressions.diff
File qs_value_expressions.diff, 4.3 KB (added by , 17 years ago) |
---|
-
django/db/models/sql/query.py
1170 1170 """ 1171 1171 return not (self.low_mark or self.high_mark) 1172 1172 1173 def add_fields(self, field _names, allow_m2m=True):1173 def add_fields(self, fields, allow_m2m=True): 1174 1174 """ 1175 1175 Adds the given (model) fields to the select set. The field names are 1176 1176 added in the order specified. 1177 1177 """ 1178 1178 alias = self.get_initial_alias() 1179 1179 opts = self.get_meta() 1180 auto_group = False 1181 group_fields = [] 1180 1182 try: 1181 for name in field_names: 1182 u1, target, u2, joins = self.setup_joins(name.split(LOOKUP_SEP), 1183 opts, alias, False, allow_m2m, True) 1184 self.select.append((joins[-1][-1], target.column)) 1183 for f in fields: 1184 if isinstance(f, str): 1185 u1, target, u2, joins = self.setup_joins(f.split(LOOKUP_SEP), 1186 opts, alias, False, allow_m2m, True) 1187 self.select.append((joins[-1][-1], target.column)) 1188 group_fields.append((joins[-1][-1], target.column)) 1189 else: 1190 if hasattr(f, 'get_cols'): 1191 table_map = {} 1192 column_map = {} 1193 for col in f.get_cols(): 1194 u1, target, u2, joins = self.setup_joins(col.split(LOOKUP_SEP), 1195 opts, alias, False, allow_m2m, True) 1196 table_map[''] = joins[-1][-1] 1197 column_map[col] = target.column 1198 f.relabel_aliases(table_map, column_map) 1199 if hasattr(f, 'do_group'): 1200 auto_group = f.do_group() 1201 self.select.append(f) 1202 if auto_group: 1203 self.group_by.extend(group_fields) 1185 1204 except JoinError: 1186 raise FieldError("Invalid field name: '%s'" % name)1205 raise FieldError("Invalid field name: '%s'" % f) 1187 1206 1188 1207 def add_ordering(self, *ordering): 1189 1208 """ -
django/db/models/query.py
478 478 479 479 def iterator(self): 480 480 self.field_names.extend([f for f in self.query.extra_select.keys()]) 481 #field_names = [f if isinstance(f,str) else f.alias() for f in self.field_names] 481 482 for row in self.query.results_iter(): 482 483 yield dict(zip(self.field_names, row)) 483 484 … … 489 490 Called by the _clone() method after initialising the rest of the 490 491 instance. 491 492 """ 493 # field_names contains strings 494 # fields contains strings or expression objects 492 495 if self._fields: 493 496 if not self.query.extra_select: 494 field_names = list(self._fields) 497 field_names = list(f.output_alias() if hasattr(f,'output_alias') else f for f in self._fields) 498 fields = list(self._fields) 495 499 else: 496 500 field_names = [] 501 fields = [] 497 502 names = set(self.model._meta.get_all_field_names()) 498 503 for f in self._fields: 499 504 if f in names: 500 505 field_names.append(f) 506 fields.append(f) 507 elif hasattr(f, 'output_alias'): 508 field_names.append(f.output_alias()) 509 fields.append(f) 501 510 elif not self.query.extra_select.has_key(f): 502 511 raise FieldDoesNotExist('%s has no field named %r' 503 512 % (self.model._meta.object_name, f)) 504 513 else: 505 514 # Default to all fields. 506 field _names = [f.attname for f in self.model._meta.fields]515 fields = field_names = [f.attname for f in self.model._meta.fields] 507 516 508 self.query.add_fields(field _names, False)517 self.query.add_fields(fields) 509 518 self.query.default_cols = False 510 519 self.field_names = field_names 511 520