Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #4130 -- Added more self-explanatory error message when a typo …

…is made in

a queryset field argument.

We may need to fine-tune the error message based on experience down the line,
but this stands as an improvement on its own. Thanks, Ned Batchelder.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@5133 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit d943f5ae927cfa99d5fe70e70350c8b4df3e3269 1 parent 0b2f9e8
Malcolm Tredinnick authored May 01, 2007
13  django/db/models/query.py
@@ -853,6 +853,13 @@ def find_field(name, field_list, related_query):
853 853
         return None
854 854
     return matches[0]
855 855
 
  856
+def field_choices(field_list, related_query):
  857
+    if related_query:
  858
+        choices = [f.field.related_query_name() for f in field_list]
  859
+    else:
  860
+        choices = [f.name for f in field_list]
  861
+    return choices
  862
+
856 863
 def lookup_inner(path, lookup_type, value, opts, table, column):
857 864
     qn = backend.quote_name
858 865
     joins, where, params = SortedDict(), [], []
@@ -937,7 +944,11 @@ def lookup_inner(path, lookup_type, value, opts, table, column):
937 944
     except FieldFound: # Match found, loop has been shortcut.
938 945
         pass
939 946
     else: # No match found.
940  
-        raise TypeError, "Cannot resolve keyword '%s' into field" % name
  947
+        choices = field_choices(current_opts.many_to_many, False) + \
  948
+            field_choices(current_opts.get_all_related_many_to_many_objects(), True) + \
  949
+            field_choices(current_opts.get_all_related_objects(), True) + \
  950
+            field_choices(current_opts.fields, False)
  951
+        raise TypeError, "Cannot resolve keyword '%s' into field, choices are: %s" % (name, ", ".join(choices))
941 952
 
942 953
     # Check whether an intermediate join is required between current_table
943 954
     # and new_table.
2  tests/modeltests/custom_columns/models.py
@@ -71,7 +71,7 @@ class Meta:
71 71
 >>> Author.objects.filter(firstname__exact='John')
72 72
 Traceback (most recent call last):
73 73
     ...
74  
-TypeError: Cannot resolve keyword 'firstname' into field
  74
+TypeError: Cannot resolve keyword 'firstname' into field, choices are: article, id, first_name, last_name
75 75
 
76 76
 >>> a = Author.objects.get(last_name__exact='Smith')
77 77
 >>> a.first_name
4  tests/modeltests/lookup/models.py
@@ -223,11 +223,11 @@ def __str__(self):
223 223
 >>> Article.objects.filter(pub_date_year='2005').count()
224 224
 Traceback (most recent call last):
225 225
     ...
226  
-TypeError: Cannot resolve keyword 'pub_date_year' into field
  226
+TypeError: Cannot resolve keyword 'pub_date_year' into field, choices are: id, headline, pub_date
227 227
 
228 228
 >>> Article.objects.filter(headline__starts='Article')
229 229
 Traceback (most recent call last):
230 230
     ...
231  
-TypeError: Cannot resolve keyword 'headline__starts' into field
  231
+TypeError: Cannot resolve keyword 'headline__starts' into field, choices are: id, headline, pub_date
232 232
 
233 233
 """}
4  tests/modeltests/many_to_one/models.py
@@ -174,13 +174,13 @@ class Meta:
174 174
 >>> Article.objects.filter(reporter_id__exact=1)
175 175
 Traceback (most recent call last):
176 176
     ...
177  
-TypeError: Cannot resolve keyword 'reporter_id' into field
  177
+TypeError: Cannot resolve keyword 'reporter_id' into field, choices are: id, headline, pub_date, reporter
178 178
 
179 179
 # You need to specify a comparison clause
180 180
 >>> Article.objects.filter(reporter_id=1)
181 181
 Traceback (most recent call last):
182 182
     ...
183  
-TypeError: Cannot resolve keyword 'reporter_id' into field
  183
+TypeError: Cannot resolve keyword 'reporter_id' into field, choices are: id, headline, pub_date, reporter
184 184
 
185 185
 # You can also instantiate an Article by passing
186 186
 # the Reporter's ID instead of a Reporter object.
2  tests/modeltests/reverse_lookup/models.py
@@ -55,5 +55,5 @@ def __str(self):
55 55
 >>> Poll.objects.get(choice__name__exact="This is the answer")
56 56
 Traceback (most recent call last):
57 57
     ...
58  
-TypeError: Cannot resolve keyword 'choice' into field
  58
+TypeError: Cannot resolve keyword 'choice' into field, choices are: poll_choice, related_choice, id, question, creator
59 59
 """}
2  tests/regressiontests/null_queries/models.py
@@ -32,7 +32,7 @@ def __str__(self):
32 32
 >>> Choice.objects.filter(foo__exact=None) 
33 33
 Traceback (most recent call last):
34 34
 ...
35  
-TypeError: Cannot resolve keyword 'foo' into field
  35
+TypeError: Cannot resolve keyword 'foo' into field, choices are: id, poll, choice
36 36
 
37 37
 # Can't use None on anything other than __exact
38 38
 >>> Choice.objects.filter(id__gt=None)

0 notes on commit d943f5a

Please sign in to comment.
Something went wrong with that request. Please try again.