Ticket #9870: aggregation-m2m-opt.2.diff

File aggregation-m2m-opt.2.diff, 1.6 KB (added by Alex Gaynor, 15 years ago)

moved the logic into add_aggregate to mirror add_filter

  • django/db/models/sql/query.py

    diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py
    index ed21ea7..d1b22d0 100644
    a b class BaseQuery(object):  
    11941194
    11951195        elif (len(field_list) > 1 or
    11961196            field_list[0] not in [i.name for i in opts.fields]):
     1197               
     1198            join_alias = self.get_initial_alias()
     1199           
    11971200            field, target, opts, join_list, last, _ = self.setup_joins(
    1198                 field_list, opts, self.get_initial_alias(), False)
     1201                field_list, opts, join_alias, False)
    11991202            # Aggregate references a model or field that requires a join
    12001203            self.allow_nulls = True
    1201 
    1202             col = (join_list[-1], target.column)
     1204           
     1205            final = len(join_list)
     1206            penultimate = last.pop()
     1207            if penultimate == final:
     1208                penultimate = last.pop()
     1209            join_alias = join_list[-1]
     1210           
     1211            col = target.column
     1212           
     1213            while final > 1:
     1214                join = self.alias_map[join_alias]
     1215                if col != join[RHS_JOIN_COL]:
     1216                    break
     1217                self.unref_alias(join_alias)
     1218                join_alias = join[LHS_ALIAS]
     1219                col = join[LHS_JOIN_COL]
     1220                join_list = join_list[:-1]
     1221                final -= 1
     1222                if final == penultimate:
     1223                    penultimate = last.pop()
     1224           
     1225            col = (join_list[-1], col)
    12031226            aggregate = aggregate_expr.add_to_query(self, aggregates,
    12041227                col=col,
    12051228                source=target,
Back to Top