diff options
| author | Andriy Sokolovskiy <me@asokolovskiy.com> | 2015-06-05 11:04:45 +0100 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2015-06-05 11:03:41 -0400 |
| commit | 469f1e362bb9670b174b37da9edd4631aff7badb (patch) | |
| tree | ae6d169a9f579b504dc4d17bdf6b2ad45cb3ccd0 /django | |
| parent | 7dcfbb2ef38bb806531d6e73dd179d5266f48d7a (diff) | |
[1.8.x] Fixed #24833 -- Fixed Case expressions with exclude().
Diffstat (limited to 'django')
| -rw-r--r-- | django/db/models/query_utils.py | 14 | ||||
| -rw-r--r-- | django/db/models/sql/query.py | 18 |
2 files changed, 24 insertions, 8 deletions
diff --git a/django/db/models/query_utils.py b/django/db/models/query_utils.py index eca060e667..16496b7fad 100644 --- a/django/db/models/query_utils.py +++ b/django/db/models/query_utils.py @@ -263,3 +263,17 @@ def refs_aggregate(lookup_parts, aggregates): if level_n_lookup in aggregates and aggregates[level_n_lookup].contains_aggregate: return aggregates[level_n_lookup], lookup_parts[n:] return False, () + + +def refs_expression(lookup_parts, annotations): + """ + A helper method to check if the lookup_parts contains references + to the given annotations set. Because the LOOKUP_SEP is contained in the + default annotation names we must check each prefix of the lookup_parts + for a match. + """ + for n in range(len(lookup_parts) + 1): + level_n_lookup = LOOKUP_SEP.join(lookup_parts[0:n]) + if level_n_lookup in annotations and annotations[level_n_lookup]: + return annotations[level_n_lookup], lookup_parts[n:] + return False, () diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py index 1a86266e6c..d0b56449b2 100644 --- a/django/db/models/sql/query.py +++ b/django/db/models/sql/query.py @@ -17,7 +17,9 @@ from django.db import DEFAULT_DB_ALIAS, connections from django.db.models.aggregates import Count from django.db.models.constants import LOOKUP_SEP from django.db.models.expressions import Col, Ref -from django.db.models.query_utils import Q, PathInfo, refs_aggregate +from django.db.models.query_utils import ( + Q, PathInfo, refs_aggregate, refs_expression, +) from django.db.models.sql.constants import ( INNER, LOUTER, ORDER_DIR, ORDER_PATTERN, QUERY_TERMS, SINGLE, ) @@ -1027,9 +1029,9 @@ class Query(object): """ lookup_splitted = lookup.split(LOOKUP_SEP) if self._annotations: - aggregate, aggregate_lookups = refs_aggregate(lookup_splitted, self.annotations) - if aggregate: - return aggregate_lookups, (), aggregate + expression, expression_lookups = refs_expression(lookup_splitted, self.annotations) + if expression: + return expression_lookups, (), expression _, field, _, lookup_parts = self.names_to_path(lookup_splitted, self.get_meta()) field_parts = lookup_splitted[0:len(lookup_splitted) - len(lookup_parts)] if len(lookup_parts) == 0: @@ -1144,7 +1146,7 @@ class Query(object): arg, value = filter_expr if not arg: raise FieldError("Cannot parse keyword query %r" % arg) - lookups, parts, reffed_aggregate = self.solve_lookup_type(arg) + lookups, parts, reffed_expression = self.solve_lookup_type(arg) if not allow_joins and len(parts) > 1: raise FieldError("Joined field references are not permitted in this query") @@ -1153,12 +1155,12 @@ class Query(object): value, lookups, used_joins = self.prepare_lookup_value(value, lookups, can_reuse, allow_joins) clause = self.where_class() - if reffed_aggregate: - condition = self.build_lookup(lookups, reffed_aggregate, value) + if reffed_expression: + condition = self.build_lookup(lookups, reffed_expression, value) if not condition: # Backwards compat for custom lookups assert len(lookups) == 1 - condition = (reffed_aggregate, lookups[0], value) + condition = (reffed_expression, lookups[0], value) clause.add(condition, AND) return clause, [] |
