From 14986a0b9d5ef9e89eb9b9cac561d5baab89f123 Mon Sep 17 00:00:00 2001 From: Mariusz Felisiak Date: Tue, 27 Sep 2016 21:00:36 +0200 Subject: [PATCH] Fixed #27268 -- Restored an invalid related field lookup error message in QuerySet filtering. --- django/db/models/sql/query.py | 2 ++ tests/lookup/tests.py | 3 +++ 2 files changed, 5 insertions(+) diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py index e6516194dd..408b697b48 100644 --- a/django/db/models/sql/query.py +++ b/django/db/models/sql/query.py @@ -1192,6 +1192,8 @@ class Query(object): raise FieldError('Related Field got invalid lookup: {}'.format(lookups[0])) assert num_lookups > 0 # Likely a bug in Django if this fails. lookup_class = field.get_lookup(lookups[0]) + if lookup_class is None: + raise FieldError('Related Field got invalid lookup: {}'.format(lookups[0])) if len(targets) == 1: lhs = targets[0].get_col(alias, field) else: diff --git a/tests/lookup/tests.py b/tests/lookup/tests.py index c1b4e9be51..0f5b72f051 100644 --- a/tests/lookup/tests.py +++ b/tests/lookup/tests.py @@ -515,6 +515,9 @@ class LookupTests(TestCase): msg = 'Related Field got invalid lookup: editor' with self.assertRaisesMessage(FieldError, msg): Article.objects.filter(author__editor__name='James') + msg = 'Related Field got invalid lookup: foo' + with self.assertRaisesMessage(FieldError, msg): + Tag.objects.filter(articles__foo='bar') def test_regex(self): # Create some articles with a bit more interesting headlines for testing field lookups: