Fixed #23443 -- Corrected erroneous FieldError message.

Thanks Tim Graham for the review.
This commit is contained in:
Thomas Chaumeny 2014-09-07 00:13:46 +02:00 committed by Loic Bistuer
parent abc11b0a33
commit e008a10c2f
2 changed files with 16 additions and 8 deletions

View File

@ -1370,9 +1370,13 @@ class Query(object):
try: try:
field, model, direct, m2m = opts.get_field_by_name(name) field, model, direct, m2m = opts.get_field_by_name(name)
except FieldDoesNotExist: except FieldDoesNotExist:
# We didn't found the current field, so move position back # We didn't find the current field, so move position back
# one step. # one step.
pos -= 1 pos -= 1
if pos == -1 or fail_on_missing:
available = opts.get_all_field_names() + list(self.aggregate_select)
raise FieldError("Cannot resolve keyword %r into field. "
"Choices are: %s" % (name, ", ".join(available)))
break break
# Check if we need any joins for concrete inheritance cases (the # Check if we need any joins for concrete inheritance cases (the
# field lives in parent, but we are currently in one of its # field lives in parent, but we are currently in one of its
@ -1412,16 +1416,13 @@ class Query(object):
# Local non-relational field. # Local non-relational field.
final_field = field final_field = field
targets = (field,) targets = (field,)
if fail_on_missing and pos + 1 != len(names):
raise FieldError(
"Cannot resolve keyword %r into field. Join on '%s'"
" not permitted." % (names[pos + 1], name))
break break
if pos == -1 or (fail_on_missing and pos + 1 != len(names)):
self.raise_field_error(opts, name)
return path, final_field, targets, names[pos + 1:] return path, final_field, targets, names[pos + 1:]
def raise_field_error(self, opts, name):
available = opts.get_all_field_names() + list(self.aggregate_select)
raise FieldError("Cannot resolve keyword %r into field. "
"Choices are: %s" % (name, ", ".join(available)))
def setup_joins(self, names, opts, alias, can_reuse=None, allow_many=True): def setup_joins(self, names, opts, alias, can_reuse=None, allow_many=True):
""" """
Compute the necessary table joins for the passage through the fields Compute the necessary table joins for the passage through the fields

View File

@ -2164,6 +2164,13 @@ class ValuesQuerysetTests(BaseQuerysetTest):
qs = qs.values_list('num', flat=True) qs = qs.values_list('num', flat=True)
self.assertQuerysetEqual(qs, [72], self.identity) self.assertQuerysetEqual(qs, [72], self.identity)
def test_field_error_values_list(self):
# see #23443
with self.assertRaisesMessage(FieldError,
"Cannot resolve keyword %r into field."
" Join on 'name' not permitted." % 'foo'):
Tag.objects.values_list('name__foo')
class QuerySetSupportsPythonIdioms(TestCase): class QuerySetSupportsPythonIdioms(TestCase):