From 43b4a1618ed85151494d68778a1bb6304ce1cb79 Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Wed, 19 Apr 2017 10:24:22 +0200 Subject: [PATCH] Fixed #28096 -- Allowed prefetch calls with ModelIterable subclasses Regression in 7ec330eeb96d0874949eacb8ed1bbb97e11807e1. Thanks Tim Graham for the review. --- django/db/models/query.py | 2 +- docs/releases/1.11.1.txt | 3 +++ tests/prefetch_related/models.py | 12 ++++++++++++ tests/prefetch_related/tests.py | 7 +++++-- 4 files changed, 21 insertions(+), 3 deletions(-) diff --git a/django/db/models/query.py b/django/db/models/query.py index 67c1f83d13..abb95ba74a 100644 --- a/django/db/models/query.py +++ b/django/db/models/query.py @@ -1260,7 +1260,7 @@ class Prefetch: 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: + if queryset is not None and not issubclass(queryset._iterable_class, ModelIterable): raise ValueError('Prefetch querysets cannot use values().') if to_attr: self.prefetch_to = LOOKUP_SEP.join(lookup.split(LOOKUP_SEP)[:-1] + [to_attr]) diff --git a/docs/releases/1.11.1.txt b/docs/releases/1.11.1.txt index 26ff892731..d60c9e33e4 100644 --- a/docs/releases/1.11.1.txt +++ b/docs/releases/1.11.1.txt @@ -33,3 +33,6 @@ Bugfixes * Fixed layout of ``ReadOnlyPasswordHashWidget`` (used in the admin's user change page) (:ticket:`28097`). + +* Allowed prefetch calls on managers with custom ``ModelIterable`` subclasses + (:ticket:`28096`). diff --git a/tests/prefetch_related/models.py b/tests/prefetch_related/models.py index a7ac55ce10..8b6b82a31a 100644 --- a/tests/prefetch_related/models.py +++ b/tests/prefetch_related/models.py @@ -5,6 +5,7 @@ from django.contrib.contenttypes.fields import ( ) from django.contrib.contenttypes.models import ContentType from django.db import models +from django.db.models.query import ModelIterable, QuerySet from django.utils.functional import cached_property @@ -95,6 +96,16 @@ class Qualification(models.Model): ordering = ['id'] +class ModelIterableSubclass(ModelIterable): + pass + + +class TeacherQuerySet(QuerySet): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self._iterable_class = ModelIterableSubclass + + class TeacherManager(models.Manager): def get_queryset(self): return super().get_queryset().prefetch_related('qualifications') @@ -105,6 +116,7 @@ class Teacher(models.Model): qualifications = models.ManyToManyField(Qualification) objects = TeacherManager() + objects_custom = TeacherQuerySet.as_manager() def __str__(self): return "%s (%s)" % (self.name, ", ".join(q.name for q in self.qualifications.all())) diff --git a/tests/prefetch_related/tests.py b/tests/prefetch_related/tests.py index d8939242f5..5ebd39db87 100644 --- a/tests/prefetch_related/tests.py +++ b/tests/prefetch_related/tests.py @@ -9,8 +9,8 @@ from django.test.utils import CaptureQueriesContext from .models import ( Author, Author2, AuthorAddress, AuthorWithAge, Bio, Book, Bookmark, BookReview, BookWithYear, Comment, Department, Employee, FavoriteAuthors, - House, LessonEntry, Person, Qualification, Reader, Room, TaggedItem, - Teacher, WordEntry, + House, LessonEntry, ModelIterableSubclass, Person, Qualification, Reader, + Room, TaggedItem, Teacher, WordEntry, ) @@ -708,6 +708,9 @@ class CustomPrefetchTests(TestCase): def test_values_queryset(self): with self.assertRaisesMessage(ValueError, 'Prefetch querysets cannot use values().'): Prefetch('houses', House.objects.values('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_to_attr_doesnt_cache_through_attr_as_list(self): house = House.objects.prefetch_related(