summaryrefslogtreecommitdiff
path: root/django/db/models/sql/query.py
diff options
context:
space:
mode:
authorMatthew Schinckel <matt@schinckel.net>2017-02-27 19:31:52 +1030
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2019-08-29 09:45:29 +0200
commit4137fc2efce2dde48340728b8006fc6d66b9e3a5 (patch)
treedf3632a53ff2d1f7efccd501880601f29e06d54c /django/db/models/sql/query.py
parent069bee7c1232458a0f13c2e30daa8df99dbd3680 (diff)
Fixed #25367 -- Allowed boolean expressions in QuerySet.filter() and exclude().
This allows using expressions that have an output_field that is a BooleanField to be used directly in a queryset filters, or in the When() clauses of a Case() expression. Thanks Josh Smeaton, Tim Graham, Simon Charette, Mariusz Felisiak, and Adam Johnson for reviews. Co-Authored-By: NyanKiyoshi <hello@vanille.bid>
Diffstat (limited to 'django/db/models/sql/query.py')
-rw-r--r--django/db/models/sql/query.py10
1 files changed, 10 insertions, 0 deletions
diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py
index 4ad1fb3f36..35f3c5e1ea 100644
--- a/django/db/models/sql/query.py
+++ b/django/db/models/sql/query.py
@@ -1229,6 +1229,16 @@ class Query(BaseExpression):
"""
if isinstance(filter_expr, dict):
raise FieldError("Cannot parse keyword query as dict")
+ if hasattr(filter_expr, 'resolve_expression') and getattr(filter_expr, 'conditional', False):
+ if connections[DEFAULT_DB_ALIAS].ops.conditional_expression_supported_in_where_clause(filter_expr):
+ condition = filter_expr.resolve_expression(self)
+ else:
+ # Expression is not supported in the WHERE clause, add
+ # comparison with True.
+ condition = self.build_lookup(['exact'], filter_expr.resolve_expression(self), True)
+ clause = self.where_class()
+ clause.add(condition, AND)
+ return clause, []
arg, value = filter_expr
if not arg:
raise FieldError("Cannot parse keyword query %r" % arg)