diff options
| author | varunkasyap <varunkasyap@hotmail.com> | 2026-04-02 14:56:45 +0530 |
|---|---|---|
| committer | Jacob Walls <jacobtylerwalls@gmail.com> | 2026-04-03 09:37:32 -0400 |
| commit | 3b161e60964aff99eddcd2627a486d81c1836b3a (patch) | |
| tree | d6fe0a8d107a98826ba8b4b1d632acedf2d7f960 /django | |
| parent | 123fa3a3f38abdb73055acc9a2cbbe3537f9323a (diff) | |
Fixed #37016 -- Avoided propagating invalid arguments from When() to Q().
Diffstat (limited to 'django')
| -rw-r--r-- | django/db/models/expressions.py | 5 | ||||
| -rw-r--r-- | django/db/models/query.py | 4 | ||||
| -rw-r--r-- | django/db/models/query_utils.py | 2 |
3 files changed, 7 insertions, 4 deletions
diff --git a/django/db/models/expressions.py b/django/db/models/expressions.py index 0c58e7749c..6c11830d9c 100644 --- a/django/db/models/expressions.py +++ b/django/db/models/expressions.py @@ -12,7 +12,7 @@ from django.core.exceptions import EmptyResultSet, FieldError, FullResultSet from django.db import DatabaseError, NotSupportedError, connection from django.db.models import fields from django.db.models.constants import LOOKUP_SEP -from django.db.models.query_utils import Q +from django.db.models.query_utils import PROHIBITED_FILTER_KWARGS, Q from django.utils.deconstruct import deconstructible from django.utils.functional import cached_property, classproperty from django.utils.hashable import make_hashable @@ -1640,6 +1640,9 @@ class When(Expression): def __init__(self, condition=None, then=None, **lookups): if lookups: + if invalid_kwargs := PROHIBITED_FILTER_KWARGS.intersection(lookups): + invalid_str = ", ".join(f"'{k}'" for k in sorted(invalid_kwargs)) + raise TypeError(f"The following kwargs are invalid: {invalid_str}") if condition is None: condition, lookups = Q(**lookups), None elif getattr(condition, "conditional", False): diff --git a/django/db/models/query.py b/django/db/models/query.py index dca504e441..43b55a76d0 100644 --- a/django/db/models/query.py +++ b/django/db/models/query.py @@ -29,7 +29,7 @@ from django.db.models.deletion import Collector from django.db.models.expressions import Case, DatabaseDefault, F, OrderBy, Value, When from django.db.models.fetch_modes import FETCH_ONE from django.db.models.functions import Cast, Trunc -from django.db.models.query_utils import FilteredRelation, Q +from django.db.models.query_utils import PROHIBITED_FILTER_KWARGS, FilteredRelation, Q from django.db.models.sql.constants import GET_ITERATOR_CHUNK_SIZE, ROW_COUNT from django.db.models.utils import ( AltersData, @@ -46,8 +46,6 @@ MAX_GET_RESULTS = 21 # The maximum number of items to display in a QuerySet.__repr__ REPR_OUTPUT_SIZE = 20 -PROHIBITED_FILTER_KWARGS = frozenset(["_connector", "_negated"]) - class BaseIterable: def __init__( diff --git a/django/db/models/query_utils.py b/django/db/models/query_utils.py index 8920977cd2..a17274fba0 100644 --- a/django/db/models/query_utils.py +++ b/django/db/models/query_utils.py @@ -21,6 +21,8 @@ from django.utils.hashable import make_hashable logger = logging.getLogger("django.db.models") +PROHIBITED_FILTER_KWARGS = frozenset(["_connector", "_negated"]) + # PathInfo is used when converting lookups (fk__somecol). The contents # describe the relation in Model terms (model Options and Fields for both # sides of the relation. The join_field is the field backing the relation. |
