From d6505273cd889886caca57884fa79941b18c2ea6 Mon Sep 17 00:00:00 2001 From: Mariusz Felisiak Date: Mon, 9 Dec 2019 15:32:09 +0100 Subject: [PATCH] Refs #31044 -- Fixed error message when using Prefetch with a values_list() queryset. --- django/db/models/query.py | 3 ++- tests/prefetch_related/tests.py | 6 ++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/django/db/models/query.py b/django/db/models/query.py index 764d3ea289..38c13584d1 100644 --- a/django/db/models/query.py +++ b/django/db/models/query.py @@ -1541,7 +1541,8 @@ class Prefetch: ) ): raise ValueError( - 'Prefetch querysets cannot use raw() and values().' + 'Prefetch querysets cannot use raw(), values(), and ' + 'values_list().' ) if to_attr: self.prefetch_to = LOOKUP_SEP.join(lookup.split(LOOKUP_SEP)[:-1] + [to_attr]) diff --git a/tests/prefetch_related/tests.py b/tests/prefetch_related/tests.py index 35c2a1c6b8..226dfb8ab9 100644 --- a/tests/prefetch_related/tests.py +++ b/tests/prefetch_related/tests.py @@ -815,15 +815,17 @@ class CustomPrefetchTests(TestCase): self.traverse_qs(list(houses), [['occupants', 'houses', 'main_room']]) 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): 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 self.assertIs(Teacher.objects_custom.all()._iterable_class, ModelIterableSubclass) Prefetch('teachers', Teacher.objects_custom.all()) 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): Prefetch('houses', House.objects.raw('select pk from house'))