Refs #31044 -- Fixed error message when using Prefetch with a values_list() queryset.

This commit is contained in:
Mariusz Felisiak 2019-12-09 15:32:09 +01:00
parent 4540842bc3
commit d6505273cd
2 changed files with 6 additions and 3 deletions

View File

@ -1541,7 +1541,8 @@ class Prefetch:
) )
): ):
raise ValueError( raise ValueError(
'Prefetch querysets cannot use raw() and values().' 'Prefetch querysets cannot use raw(), values(), and '
'values_list().'
) )
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

@ -815,15 +815,17 @@ class CustomPrefetchTests(TestCase):
self.traverse_qs(list(houses), [['occupants', 'houses', 'main_room']]) self.traverse_qs(list(houses), [['occupants', 'houses', 'main_room']])
def test_values_queryset(self): def test_values_queryset(self):
msg = 'Prefetch querysets cannot use raw() and values().' msg = 'Prefetch querysets cannot use raw(), values(), and values_list().'
with self.assertRaisesMessage(ValueError, msg): with self.assertRaisesMessage(ValueError, msg):
Prefetch('houses', House.objects.values('pk')) Prefetch('houses', House.objects.values('pk'))
with self.assertRaisesMessage(ValueError, msg):
Prefetch('houses', House.objects.values_list('pk'))
# That error doesn't affect managers with custom ModelIterable subclasses # That error doesn't affect managers with custom ModelIterable subclasses
self.assertIs(Teacher.objects_custom.all()._iterable_class, ModelIterableSubclass) self.assertIs(Teacher.objects_custom.all()._iterable_class, ModelIterableSubclass)
Prefetch('teachers', Teacher.objects_custom.all()) Prefetch('teachers', Teacher.objects_custom.all())
def test_raw_queryset(self): def test_raw_queryset(self):
msg = 'Prefetch querysets cannot use raw() and values().' msg = 'Prefetch querysets cannot use raw(), values(), and values_list().'
with self.assertRaisesMessage(ValueError, msg): with self.assertRaisesMessage(ValueError, msg):
Prefetch('houses', House.objects.raw('select pk from house')) Prefetch('houses', House.objects.raw('select pk from house'))