diff options
| author | Russell Keith-Magee <russell@keith-magee.com> | 2009-04-08 13:19:48 +0000 |
|---|---|---|
| committer | Russell Keith-Magee <russell@keith-magee.com> | 2009-04-08 13:19:48 +0000 |
| commit | 98ef7e85bdaad4b21c31b425a62b2c8bc294be48 (patch) | |
| tree | cde55ff029b2342caec0043500db924c18a05c38 /django/db/models/sql | |
| parent | aa9a40b0d8442170cfa3a915c84d6ca687bb93d5 (diff) | |
Fixed #10666 -- Corrected the handling of inherited fields with aggregate() and annotate(). Thanks to julienb for the report.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10446 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/db/models/sql')
| -rw-r--r-- | django/db/models/sql/query.py | 24 |
1 files changed, 14 insertions, 10 deletions
diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py index cafde11415..256cbef8e1 100644 --- a/django/db/models/sql/query.py +++ b/django/db/models/sql/query.py @@ -1403,8 +1403,17 @@ class BaseQuery(object): field_name = field_list[0] col = field_name source = self.aggregates[field_name] - elif (len(field_list) > 1 or - field_list[0] not in [i.name for i in opts.fields]): + elif ((len(field_list) > 1) or + (field_list[0] not in [i.name for i in opts.fields]) or + self.group_by is None or + not is_summary): + # If: + # - the field descriptor has more than one part (foo__bar), or + # - the field descriptor is referencing an m2m/m2o field, or + # - this is a reference to a model field (possibly inherited), or + # - this is an annotation over a model field + # then we need to explore the joins that are required. + field, source, opts, join_list, last, _ = self.setup_joins( field_list, opts, self.get_initial_alias(), False) @@ -1419,15 +1428,11 @@ class BaseQuery(object): col = (join_list[-1], col) else: - # Aggregate references a normal field + # The simplest cases. No joins required - + # just reference the provided column alias. field_name = field_list[0] source = opts.get_field(field_name) - if not (self.group_by is not None and is_summary): - # Only use a column alias if this is a - # standalone aggregate, or an annotation - col = (opts.db_table, source.column) - else: - col = field_name + col = field_name # Add the aggregate to the query alias = truncate_name(alias, self.connection.ops.max_name_length()) @@ -1659,7 +1664,6 @@ class BaseQuery(object): last.append(len(joins)) if name == 'pk': name = opts.pk.name - try: field, model, direct, m2m = opts.get_field_by_name(name) except FieldDoesNotExist: |
