Loading django/db/models/query.py +2 −0 Original line number Diff line number Diff line Loading @@ -1331,6 +1331,8 @@ class Prefetch(object): self.prefetch_through = lookup # `prefetch_to` is the path to the attribute that stores the result. 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: self.prefetch_to = LOOKUP_SEP.join(lookup.split(LOOKUP_SEP)[:-1] + [to_attr]) Loading tests/prefetch_related/tests.py +4 −0 Original line number Diff line number Diff line Loading @@ -733,6 +733,10 @@ class CustomPrefetchTests(TestCase): 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): Loading Loading
django/db/models/query.py +2 −0 Original line number Diff line number Diff line Loading @@ -1331,6 +1331,8 @@ class Prefetch(object): self.prefetch_through = lookup # `prefetch_to` is the path to the attribute that stores the result. 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: self.prefetch_to = LOOKUP_SEP.join(lookup.split(LOOKUP_SEP)[:-1] + [to_attr]) Loading
tests/prefetch_related/tests.py +4 −0 Original line number Diff line number Diff line Loading @@ -733,6 +733,10 @@ class CustomPrefetchTests(TestCase): 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): Loading