diff --git a/django/db/models/expressions.py b/django/db/models/expressions.py index 0c58e7749c24..6c11830d9c49 100644 --- a/django/db/models/expressions.py +++ b/django/db/models/expressions.py @@ -12,7 +12,7 @@ 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 dca504e44179..43b55a76d0cc 100644 --- a/django/db/models/query.py +++ b/django/db/models/query.py @@ -29,7 +29,7 @@ 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 @@ # 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 8920977cd270..a17274fba0dc 100644 --- a/django/db/models/query_utils.py +++ b/django/db/models/query_utils.py @@ -21,6 +21,8 @@ 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. diff --git a/tests/expressions_case/tests.py b/tests/expressions_case/tests.py index 8704a7b9919f..1fde24f14924 100644 --- a/tests/expressions_case/tests.py +++ b/tests/expressions_case/tests.py @@ -1668,6 +1668,11 @@ def test_invalid_when_constructor_args(self): with self.assertRaisesMessage(TypeError, msg): When() + def test_when_rejects_invalid_arguments(self): + msg = "The following kwargs are invalid: '_connector', '_negated'" + with self.assertRaisesMessage(TypeError, msg): + When(_negated=True, _connector="evil") + def test_empty_q_object(self): msg = "An empty Q() can't be used as a When() condition." with self.assertRaisesMessage(ValueError, msg): diff --git a/tests/modeladmin/tests.py b/tests/modeladmin/tests.py index 2516ca32071f..74bbc83efb07 100644 --- a/tests/modeladmin/tests.py +++ b/tests/modeladmin/tests.py @@ -849,7 +849,8 @@ class ConcertAdmin(ModelAdmin): 'class="related-widget-wrapper" data-model-ref="band">" ) self.assertInHTML(expected, cmafa().render()) @@ -873,9 +874,10 @@ class ConcertAdmin(ModelAdmin): expected = ( '
Main band:
' + 'for="id_main_band_0">The Doors' + "" ) self.assertInHTML(expected, cmafa().render())