mirror of
https://github.com/django/django.git
synced 2025-10-31 09:41:08 +00:00
Fixed #30484 -- Added conditional expressions support to CheckConstraint.
This commit is contained in:
committed by
Mariusz Felisiak
parent
37e6c5b79b
commit
e9a0e1d4f6
@@ -30,6 +30,11 @@ class BaseConstraint:
|
||||
class CheckConstraint(BaseConstraint):
|
||||
def __init__(self, *, check, name):
|
||||
self.check = check
|
||||
if not getattr(check, 'conditional', False):
|
||||
raise TypeError(
|
||||
'CheckConstraint.check must be a Q instance or boolean '
|
||||
'expression.'
|
||||
)
|
||||
super().__init__(name)
|
||||
|
||||
def _get_check_sql(self, model, schema_editor):
|
||||
|
||||
@@ -1221,8 +1221,19 @@ class Query(BaseExpression):
|
||||
"""
|
||||
if isinstance(filter_expr, dict):
|
||||
raise FieldError("Cannot parse keyword query as dict")
|
||||
if isinstance(filter_expr, Q):
|
||||
return self._add_q(
|
||||
filter_expr,
|
||||
branch_negated=branch_negated,
|
||||
current_negated=current_negated,
|
||||
used_aliases=can_reuse,
|
||||
allow_joins=allow_joins,
|
||||
split_subq=split_subq,
|
||||
)
|
||||
if hasattr(filter_expr, 'resolve_expression') and getattr(filter_expr, 'conditional', False):
|
||||
condition = self.build_lookup(['exact'], filter_expr.resolve_expression(self), True)
|
||||
condition = self.build_lookup(
|
||||
['exact'], filter_expr.resolve_expression(self, allow_joins=allow_joins), True
|
||||
)
|
||||
clause = self.where_class()
|
||||
clause.add(condition, AND)
|
||||
return clause, []
|
||||
@@ -1332,8 +1343,8 @@ class Query(BaseExpression):
|
||||
self.where.add(clause, AND)
|
||||
self.demote_joins(existing_inner)
|
||||
|
||||
def build_where(self, q_object):
|
||||
return self._add_q(q_object, used_aliases=set(), allow_joins=False)[0]
|
||||
def build_where(self, filter_expr):
|
||||
return self.build_filter(filter_expr, allow_joins=False)[0]
|
||||
|
||||
def _add_q(self, q_object, used_aliases, branch_negated=False,
|
||||
current_negated=False, allow_joins=True, split_subq=True):
|
||||
@@ -1345,18 +1356,12 @@ class Query(BaseExpression):
|
||||
negated=q_object.negated)
|
||||
joinpromoter = JoinPromoter(q_object.connector, len(q_object.children), current_negated)
|
||||
for child in q_object.children:
|
||||
if isinstance(child, Node):
|
||||
child_clause, needed_inner = self._add_q(
|
||||
child, used_aliases, branch_negated,
|
||||
current_negated, allow_joins, split_subq)
|
||||
joinpromoter.add_votes(needed_inner)
|
||||
else:
|
||||
child_clause, needed_inner = self.build_filter(
|
||||
child, can_reuse=used_aliases, branch_negated=branch_negated,
|
||||
current_negated=current_negated, allow_joins=allow_joins,
|
||||
split_subq=split_subq,
|
||||
)
|
||||
joinpromoter.add_votes(needed_inner)
|
||||
child_clause, needed_inner = self.build_filter(
|
||||
child, can_reuse=used_aliases, branch_negated=branch_negated,
|
||||
current_negated=current_negated, allow_joins=allow_joins,
|
||||
split_subq=split_subq,
|
||||
)
|
||||
joinpromoter.add_votes(needed_inner)
|
||||
if child_clause:
|
||||
target_clause.add(child_clause, connector)
|
||||
needed_inner = joinpromoter.update_join_types(self)
|
||||
|
||||
Reference in New Issue
Block a user