Loading django/db/models/sql/query.py +4 −1 Original line number Diff line number Diff line Loading @@ -1172,7 +1172,10 @@ class Query(object): if field.is_relation: # No support for transforms for relational fields assert len(lookups) == 1 num_lookups = len(lookups) if num_lookups > 1: 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 len(targets) == 1: lhs = targets[0].get_col(alias, field) Loading tests/lookup/tests.py +6 −0 Original line number Diff line number Diff line Loading @@ -493,6 +493,12 @@ class LookupTests(TestCase): str(ex), "Unsupported lookup 'starts' for CharField " "or join on the field not permitted.") def test_relation_nested_lookup_error(self): # An invalid nested lookup on a related field raises a useful error. msg = 'Related Field got invalid lookup: editor' with self.assertRaisesMessage(FieldError, msg): Article.objects.filter(author__editor__name='James') def test_regex(self): # Create some articles with a bit more interesting headlines for testing field lookups: for a in Article.objects.all(): Loading Loading
django/db/models/sql/query.py +4 −1 Original line number Diff line number Diff line Loading @@ -1172,7 +1172,10 @@ class Query(object): if field.is_relation: # No support for transforms for relational fields assert len(lookups) == 1 num_lookups = len(lookups) if num_lookups > 1: 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 len(targets) == 1: lhs = targets[0].get_col(alias, field) Loading
tests/lookup/tests.py +6 −0 Original line number Diff line number Diff line Loading @@ -493,6 +493,12 @@ class LookupTests(TestCase): str(ex), "Unsupported lookup 'starts' for CharField " "or join on the field not permitted.") def test_relation_nested_lookup_error(self): # An invalid nested lookup on a related field raises a useful error. msg = 'Related Field got invalid lookup: editor' with self.assertRaisesMessage(FieldError, msg): Article.objects.filter(author__editor__name='James') def test_regex(self): # Create some articles with a bit more interesting headlines for testing field lookups: for a in Article.objects.all(): Loading