diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py index 74eec722910..81f6f196d34 100644 --- a/django/db/models/sql/query.py +++ b/django/db/models/sql/query.py @@ -1594,10 +1594,13 @@ class Query: else: field_list = name.split(LOOKUP_SEP) join_info = self.setup_joins(field_list, self.get_meta(), self.get_initial_alias(), can_reuse=reuse) - targets, _, join_list = self.trim_joins(join_info.targets, join_info.joins, join_info.path) + targets, final_alias, join_list = self.trim_joins(join_info.targets, join_info.joins, join_info.path) if len(targets) > 1: raise FieldError("Referencing multicolumn fields with F() objects " "isn't supported") + # Verify that the last lookup in name is a field or a transform: + # transform_function() raises FieldError if not. + join_info.transform_function(targets[0], final_alias) if reuse is not None: reuse.update(join_list) col = _get_col(targets[0], join_info.targets[0], join_list[-1], simple_col) diff --git a/docs/releases/2.1.2.txt b/docs/releases/2.1.2.txt index 4ac5e25ef77..17c50fffd7e 100644 --- a/docs/releases/2.1.2.txt +++ b/docs/releases/2.1.2.txt @@ -9,4 +9,5 @@ Django 2.1.2 fixes several bugs in 2.1.1 Bugfixes ======== -* ... +* Fixed a regression where nonexistent joins in ``F()`` no longer raised + ``FieldError`` (:ticket:`29727`). diff --git a/tests/expressions/tests.py b/tests/expressions/tests.py index 973176e223c..9a646088b14 100644 --- a/tests/expressions/tests.py +++ b/tests/expressions/tests.py @@ -595,6 +595,10 @@ class BasicExpressionsTests(TestCase): with self.assertRaisesMessage(FieldError, "Cannot resolve keyword 'nope' into field."): list(Employee.objects.filter(firstname=F('nope'))) + def test_incorrect_joined_field_in_F_expression(self): + with self.assertRaisesMessage(FieldError, "Cannot resolve keyword 'nope' into field."): + list(Company.objects.filter(ceo__pk=F('point_of_contact__nope'))) + class IterableLookupInnerExpressionsTests(TestCase): @classmethod