From be6a309b1d749c47821dfdc5add6576f2c61cda0 Mon Sep 17 00:00:00 2001 From: Mariusz Felisiak Date: Tue, 28 Mar 2023 19:18:48 +0200 Subject: [PATCH] [4.2.x] Refs #29799 -- Added field instance lookups to suggestions in FieldErrors. Bug in cd1afd553f9c175ebccfc0f50e72b43b9604bd97. Backport of 3afdc9e9b47d5bdd1bd653633b4cb2357478ade5 from main --- django/db/models/sql/query.py | 2 +- tests/lookup/tests.py | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py index 3fe00002bd..fa9cc3b4e5 100644 --- a/django/db/models/sql/query.py +++ b/django/db/models/sql/query.py @@ -1300,7 +1300,7 @@ class Query(BaseExpression): else: output_field = lhs.output_field.__class__ suggested_lookups = difflib.get_close_matches( - name, output_field.get_lookups() + name, lhs.output_field.get_lookups() ) if suggested_lookups: suggestion = ", perhaps you meant %s?" % " or ".join(suggested_lookups) diff --git a/tests/lookup/tests.py b/tests/lookup/tests.py index 53eb76d174..89fd582a53 100644 --- a/tests/lookup/tests.py +++ b/tests/lookup/tests.py @@ -19,7 +19,7 @@ from django.db.models import ( Value, When, ) -from django.db.models.functions import Cast, Substr +from django.db.models.functions import Cast, Length, Substr from django.db.models.lookups import ( Exact, GreaterThan, @@ -29,7 +29,7 @@ from django.db.models.lookups import ( LessThanOrEqual, ) from django.test import TestCase, skipUnlessDBFeature -from django.test.utils import isolate_apps +from django.test.utils import isolate_apps, register_lookup from .models import ( Article, @@ -784,6 +784,16 @@ class LookupTests(TestCase): ): Article.objects.filter(pub_date__gobbledygook="blahblah") + def test_unsupported_lookups_custom_lookups(self): + slug_field = Article._meta.get_field("slug") + msg = ( + "Unsupported lookup 'lengtp' for SlugField or join on the field not " + "permitted, perhaps you meant length?" + ) + with self.assertRaisesMessage(FieldError, msg): + with register_lookup(slug_field, Length): + Article.objects.filter(slug__lengtp=20) + def test_relation_nested_lookup_error(self): # An invalid nested lookup on a related field raises a useful error. msg = (