Author: mtredinnick
Date: 2007-04-30 22:21:54 -0500 (Mon, 30 Apr 2007)
New Revision: 5133

Modified:
   django/trunk/django/db/models/query.py
   django/trunk/tests/modeltests/custom_columns/models.py
   django/trunk/tests/modeltests/lookup/models.py
   django/trunk/tests/modeltests/many_to_one/models.py
   django/trunk/tests/modeltests/reverse_lookup/models.py
   django/trunk/tests/regressiontests/null_queries/models.py
Log:
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.


Modified: django/trunk/django/db/models/query.py
===================================================================
--- django/trunk/django/db/models/query.py      2007-05-01 03:10:10 UTC (rev 
5132)
+++ django/trunk/django/db/models/query.py      2007-05-01 03:21:54 UTC (rev 
5133)
@@ -853,6 +853,13 @@
         return None
     return matches[0]
 
+def field_choices(field_list, related_query):
+    if related_query:
+        choices = [f.field.related_query_name() for f in field_list]
+    else:
+        choices = [f.name for f in field_list]
+    return choices
+
 def lookup_inner(path, lookup_type, value, opts, table, column):
     qn = backend.quote_name
     joins, where, params = SortedDict(), [], []
@@ -937,7 +944,11 @@
     except FieldFound: # Match found, loop has been shortcut.
         pass
     else: # No match found.
-        raise TypeError, "Cannot resolve keyword '%s' into field" % name
+        choices = field_choices(current_opts.many_to_many, False) + \
+            field_choices(current_opts.get_all_related_many_to_many_objects(), 
True) + \
+            field_choices(current_opts.get_all_related_objects(), True) + \
+            field_choices(current_opts.fields, False)
+        raise TypeError, "Cannot resolve keyword '%s' into field, choices are: 
%s" % (name, ", ".join(choices))
 
     # Check whether an intermediate join is required between current_table
     # and new_table.

Modified: django/trunk/tests/modeltests/custom_columns/models.py
===================================================================
--- django/trunk/tests/modeltests/custom_columns/models.py      2007-05-01 
03:10:10 UTC (rev 5132)
+++ django/trunk/tests/modeltests/custom_columns/models.py      2007-05-01 
03:21:54 UTC (rev 5133)
@@ -71,7 +71,7 @@
 >>> Author.objects.filter(firstname__exact='John')
 Traceback (most recent call last):
     ...
-TypeError: Cannot resolve keyword 'firstname' into field
+TypeError: Cannot resolve keyword 'firstname' into field, choices are: 
article, id, first_name, last_name
 
 >>> a = Author.objects.get(last_name__exact='Smith')
 >>> a.first_name

Modified: django/trunk/tests/modeltests/lookup/models.py
===================================================================
--- django/trunk/tests/modeltests/lookup/models.py      2007-05-01 03:10:10 UTC 
(rev 5132)
+++ django/trunk/tests/modeltests/lookup/models.py      2007-05-01 03:21:54 UTC 
(rev 5133)
@@ -223,11 +223,11 @@
 >>> Article.objects.filter(pub_date_year='2005').count()
 Traceback (most recent call last):
     ...
-TypeError: Cannot resolve keyword 'pub_date_year' into field
+TypeError: Cannot resolve keyword 'pub_date_year' into field, choices are: id, 
headline, pub_date
 
 >>> Article.objects.filter(headline__starts='Article')
 Traceback (most recent call last):
     ...
-TypeError: Cannot resolve keyword 'headline__starts' into field
+TypeError: Cannot resolve keyword 'headline__starts' into field, choices are: 
id, headline, pub_date
 
 """}

Modified: django/trunk/tests/modeltests/many_to_one/models.py
===================================================================
--- django/trunk/tests/modeltests/many_to_one/models.py 2007-05-01 03:10:10 UTC 
(rev 5132)
+++ django/trunk/tests/modeltests/many_to_one/models.py 2007-05-01 03:21:54 UTC 
(rev 5133)
@@ -174,13 +174,13 @@
 >>> Article.objects.filter(reporter_id__exact=1)
 Traceback (most recent call last):
     ...
-TypeError: Cannot resolve keyword 'reporter_id' into field
+TypeError: Cannot resolve keyword 'reporter_id' into field, choices are: id, 
headline, pub_date, reporter
 
 # You need to specify a comparison clause
 >>> Article.objects.filter(reporter_id=1)
 Traceback (most recent call last):
     ...
-TypeError: Cannot resolve keyword 'reporter_id' into field
+TypeError: Cannot resolve keyword 'reporter_id' into field, choices are: id, 
headline, pub_date, reporter
 
 # You can also instantiate an Article by passing
 # the Reporter's ID instead of a Reporter object.

Modified: django/trunk/tests/modeltests/reverse_lookup/models.py
===================================================================
--- django/trunk/tests/modeltests/reverse_lookup/models.py      2007-05-01 
03:10:10 UTC (rev 5132)
+++ django/trunk/tests/modeltests/reverse_lookup/models.py      2007-05-01 
03:21:54 UTC (rev 5133)
@@ -55,5 +55,5 @@
 >>> Poll.objects.get(choice__name__exact="This is the answer")
 Traceback (most recent call last):
     ...
-TypeError: Cannot resolve keyword 'choice' into field
+TypeError: Cannot resolve keyword 'choice' into field, choices are: 
poll_choice, related_choice, id, question, creator
 """}

Modified: django/trunk/tests/regressiontests/null_queries/models.py
===================================================================
--- django/trunk/tests/regressiontests/null_queries/models.py   2007-05-01 
03:10:10 UTC (rev 5132)
+++ django/trunk/tests/regressiontests/null_queries/models.py   2007-05-01 
03:21:54 UTC (rev 5133)
@@ -32,7 +32,7 @@
 >>> Choice.objects.filter(foo__exact=None) 
 Traceback (most recent call last):
 ...
-TypeError: Cannot resolve keyword 'foo' into field
+TypeError: Cannot resolve keyword 'foo' into field, choices are: id, poll, 
choice
 
 # Can't use None on anything other than __exact
 >>> Choice.objects.filter(id__gt=None)


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to