File tree Expand file tree Collapse file tree 2 files changed +27
-1
lines changed
django_mongodb_backend/fields Expand file tree Collapse file tree 2 files changed +27
-1
lines changed Original file line number Diff line number Diff line change 1+ import difflib
2+
13from django .core import checks
24from django .core .exceptions import FieldDoesNotExist
35from django .db import models
@@ -149,9 +151,15 @@ def get_transform(self, name):
149151 result = KeyTransformFactory (name , new_field )
150152 else :
151153 if self .ref_field .get_transform (name ) is None :
154+ suggested_lookups = difflib .get_close_matches (name , self .ref_field .get_lookups ())
155+ if suggested_lookups :
156+ suggested_lookups = " or " .join (suggested_lookups )
157+ suggestion = f", perhaps you meant { suggested_lookups } ?"
158+ else :
159+ suggestion = "."
152160 raise FieldDoesNotExist (
153161 f"Unsupported lookup '{ name } ' for "
154- f"{ self .ref_field .__class__ .__name__ } '{ self .ref_field .name } '"
162+ f"{ self .ref_field .__class__ .__name__ } '{ self .ref_field .name } '{ suggestion } "
155163 )
156164 result = KeyTransformFactory (name , self .ref_field )
157165 return result
Original file line number Diff line number Diff line change @@ -119,6 +119,24 @@ def test_not_exists_in_embedded(self):
119119 with self .assertRaisesMessage (FieldDoesNotExist , msg ):
120120 Book .objects .filter (author__address__floor = "NYC" )
121121
122+ def test_unsupported_lookups (self ):
123+ msg = (
124+ "Unsupported lookup '{lookup}' for CharField 'name', "
125+ "perhaps you meant {suggested_lookups}"
126+ )
127+ with self .assertRaisesMessage (
128+ FieldDoesNotExist , msg .format (lookup = "exactly" , suggested_lookups = "exact or iexact" )
129+ ):
130+ Book .objects .filter (author__name__exactly = "NYC" ).all ()
131+ with self .assertRaisesMessage (
132+ FieldDoesNotExist , msg .format (lookup = "gti" , suggested_lookups = "gt or gte" )
133+ ):
134+ Book .objects .filter (author__name__gti = "NYC" ).all ()
135+ with self .assertRaisesMessage (
136+ FieldDoesNotExist , msg .format (lookup = "is_null" , suggested_lookups = "isnull" )
137+ ):
138+ Book .objects .filter (author__name__is_null = "NYC" ).all ()
139+
122140
123141@isolate_apps ("model_fields_" )
124142class CheckTests (SimpleTestCase ):
You can’t perform that action at this time.
0 commit comments