mirror of
https://github.com/django/django.git
synced 2025-12-01 15:53:15 +00:00
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>
This commit is contained in:
parent
069bee7c12
commit
4137fc2efc
10 changed files with 184 additions and 23 deletions
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue