Refs #26565 -- Errored nicely when using Prefetch with a values() queryset.

Thanks Maxime Lorant for the report and Anssi for the suggestion.
This commit is contained in:
Simon Charette 2016-05-03 09:28:31 -04:00
parent 8a47ba679d
commit 7ec330eeb9
2 changed files with 6 additions and 0 deletions

View File

@ -1331,6 +1331,8 @@ class Prefetch(object):
self.prefetch_through = lookup self.prefetch_through = lookup
# `prefetch_to` is the path to the attribute that stores the result. # `prefetch_to` is the path to the attribute that stores the result.
self.prefetch_to = lookup self.prefetch_to = lookup
if queryset is not None and queryset._iterable_class is not ModelIterable:
raise ValueError('Prefetch querysets cannot use values().')
if to_attr: if to_attr:
self.prefetch_to = LOOKUP_SEP.join(lookup.split(LOOKUP_SEP)[:-1] + [to_attr]) self.prefetch_to = LOOKUP_SEP.join(lookup.split(LOOKUP_SEP)[:-1] + [to_attr])

View File

@ -733,6 +733,10 @@ class CustomPrefetchTests(TestCase):
self.assertEqual(str(warns[0].message), msg) self.assertEqual(str(warns[0].message), msg)
self.assertEqual(str(warns[0].message), msg) self.assertEqual(str(warns[0].message), msg)
def test_values_queryset(self):
with self.assertRaisesMessage(ValueError, 'Prefetch querysets cannot use values().'):
Prefetch('houses', House.objects.values('pk'))
class DefaultManagerTests(TestCase): class DefaultManagerTests(TestCase):