diff options
| author | Simon Charette <charette.s@gmail.com> | 2019-11-15 00:24:21 -0500 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2019-11-21 11:56:35 +0100 |
| commit | f97a6123c07de5099fdf8b7d00ef7d20ed354e07 (patch) | |
| tree | 77ad081f3d363291bc2274d349461bb401f88efc | |
| parent | e9a0e1d4f6ecfc4227acff74e1f56f288a0b30aa (diff) | |
Refs #25367 -- Made Query.build_filter() raise TypeError on non-conditional expressions.
| -rw-r--r-- | django/db/models/sql/query.py | 4 | ||||
| -rw-r--r-- | tests/queries/test_query.py | 6 |
2 files changed, 9 insertions, 1 deletions
diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py index dcf897c649..8fee6b0183 100644 --- a/django/db/models/sql/query.py +++ b/django/db/models/sql/query.py @@ -1230,7 +1230,9 @@ class Query(BaseExpression): allow_joins=allow_joins, split_subq=split_subq, ) - if hasattr(filter_expr, 'resolve_expression') and getattr(filter_expr, 'conditional', False): + if hasattr(filter_expr, 'resolve_expression'): + if not getattr(filter_expr, 'conditional', False): + raise TypeError('Cannot filter against a non-conditional expression.') condition = self.build_lookup( ['exact'], filter_expr.resolve_expression(self, allow_joins=allow_joins), True ) diff --git a/tests/queries/test_query.py b/tests/queries/test_query.py index 9d18b15f3c..523fa607f0 100644 --- a/tests/queries/test_query.py +++ b/tests/queries/test_query.py @@ -144,3 +144,9 @@ class TestQuery(SimpleTestCase): msg = 'Joined field references are not permitted in this query' with self.assertRaisesMessage(FieldError, msg): query.build_where(filter_expr) + + def test_filter_non_conditional(self): + query = Query(Item) + msg = 'Cannot filter against a non-conditional expression.' + with self.assertRaisesMessage(TypeError, msg): + query.build_where(Func(output_field=CharField())) |
