Fixed #29727 -- Made nonexistent joins in F() raise FieldError.

Regression in 2162f0983d.
This commit is contained in:
Alexander Holmbäck 2018-09-06 20:10:20 +00:00 committed by Tim Graham
parent aa16ec5474
commit f315d0423a
3 changed files with 10 additions and 2 deletions

View File

@ -1594,10 +1594,13 @@ class Query:
else: else:
field_list = name.split(LOOKUP_SEP) field_list = name.split(LOOKUP_SEP)
join_info = self.setup_joins(field_list, self.get_meta(), self.get_initial_alias(), can_reuse=reuse) 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: if len(targets) > 1:
raise FieldError("Referencing multicolumn fields with F() objects " raise FieldError("Referencing multicolumn fields with F() objects "
"isn't supported") "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: if reuse is not None:
reuse.update(join_list) reuse.update(join_list)
col = _get_col(targets[0], join_info.targets[0], join_list[-1], simple_col) col = _get_col(targets[0], join_info.targets[0], join_list[-1], simple_col)

View File

@ -9,4 +9,5 @@ Django 2.1.2 fixes several bugs in 2.1.1
Bugfixes Bugfixes
======== ========
* ... * Fixed a regression where nonexistent joins in ``F()`` no longer raised
``FieldError`` (:ticket:`29727`).

View File

@ -595,6 +595,10 @@ class BasicExpressionsTests(TestCase):
with self.assertRaisesMessage(FieldError, "Cannot resolve keyword 'nope' into field."): with self.assertRaisesMessage(FieldError, "Cannot resolve keyword 'nope' into field."):
list(Employee.objects.filter(firstname=F('nope'))) 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): class IterableLookupInnerExpressionsTests(TestCase):
@classmethod @classmethod