diff --git a/django/db/models/query.py b/django/db/models/query.py index 91f54e48b99..06163677d4e 100644 --- a/django/db/models/query.py +++ b/django/db/models/query.py @@ -853,6 +853,13 @@ def find_field(name, field_list, related_query): 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 @@ def lookup_inner(path, lookup_type, value, opts, table, column): 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. diff --git a/tests/modeltests/custom_columns/models.py b/tests/modeltests/custom_columns/models.py index c09ca055571..b2b7261c894 100644 --- a/tests/modeltests/custom_columns/models.py +++ b/tests/modeltests/custom_columns/models.py @@ -71,7 +71,7 @@ __test__ = {'API_TESTS':""" >>> 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 diff --git a/tests/modeltests/lookup/models.py b/tests/modeltests/lookup/models.py index 106c97d3b4f..c28f0e015f0 100644 --- a/tests/modeltests/lookup/models.py +++ b/tests/modeltests/lookup/models.py @@ -223,11 +223,11 @@ DoesNotExist: Article matching query does not exist. >>> 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 """} diff --git a/tests/modeltests/many_to_one/models.py b/tests/modeltests/many_to_one/models.py index 82eb3257d0f..3ed449d5989 100644 --- a/tests/modeltests/many_to_one/models.py +++ b/tests/modeltests/many_to_one/models.py @@ -174,13 +174,13 @@ False >>> 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. diff --git a/tests/modeltests/reverse_lookup/models.py b/tests/modeltests/reverse_lookup/models.py index 7e6712676fa..d30269c5c6d 100644 --- a/tests/modeltests/reverse_lookup/models.py +++ b/tests/modeltests/reverse_lookup/models.py @@ -55,5 +55,5 @@ __test__ = {'API_TESTS':""" >>> 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 """} diff --git a/tests/regressiontests/null_queries/models.py b/tests/regressiontests/null_queries/models.py index 09024f18c20..4396ab40053 100644 --- a/tests/regressiontests/null_queries/models.py +++ b/tests/regressiontests/null_queries/models.py @@ -32,7 +32,7 @@ __test__ = {'API_TESTS':""" >>> 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)