[1.11.x] Fixed #28096 -- Allowed prefetch calls with ModelIterable subclasses
Regression in7ec330eeb9
. Thanks Tim Graham for the review. Backport of43b4a1618e
from master.
This commit is contained in:
parent
cf1e682c8c
commit
61e883c7c2
|
@ -1305,7 +1305,7 @@ 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:
|
if queryset is not None and not issubclass(queryset._iterable_class, ModelIterable):
|
||||||
raise ValueError('Prefetch querysets cannot use values().')
|
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])
|
||||||
|
|
|
@ -33,3 +33,6 @@ Bugfixes
|
||||||
|
|
||||||
* Fixed layout of ``ReadOnlyPasswordHashWidget`` (used in the admin's user
|
* Fixed layout of ``ReadOnlyPasswordHashWidget`` (used in the admin's user
|
||||||
change page) (:ticket:`28097`).
|
change page) (:ticket:`28097`).
|
||||||
|
|
||||||
|
* Allowed prefetch calls on managers with custom ``ModelIterable`` subclasses
|
||||||
|
(:ticket:`28096`).
|
||||||
|
|
|
@ -5,6 +5,7 @@ from django.contrib.contenttypes.fields import (
|
||||||
)
|
)
|
||||||
from django.contrib.contenttypes.models import ContentType
|
from django.contrib.contenttypes.models import ContentType
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
from django.db.models.query import ModelIterable, QuerySet
|
||||||
from django.utils.encoding import python_2_unicode_compatible
|
from django.utils.encoding import python_2_unicode_compatible
|
||||||
from django.utils.functional import cached_property
|
from django.utils.functional import cached_property
|
||||||
|
|
||||||
|
@ -100,6 +101,16 @@ class Qualification(models.Model):
|
||||||
ordering = ['id']
|
ordering = ['id']
|
||||||
|
|
||||||
|
|
||||||
|
class ModelIterableSubclass(ModelIterable):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class TeacherQuerySet(QuerySet):
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
super(TeacherQuerySet, self).__init__(*args, **kwargs)
|
||||||
|
self._iterable_class = ModelIterableSubclass
|
||||||
|
|
||||||
|
|
||||||
class TeacherManager(models.Manager):
|
class TeacherManager(models.Manager):
|
||||||
def get_queryset(self):
|
def get_queryset(self):
|
||||||
return super(TeacherManager, self).get_queryset().prefetch_related('qualifications')
|
return super(TeacherManager, self).get_queryset().prefetch_related('qualifications')
|
||||||
|
@ -111,6 +122,7 @@ class Teacher(models.Model):
|
||||||
qualifications = models.ManyToManyField(Qualification)
|
qualifications = models.ManyToManyField(Qualification)
|
||||||
|
|
||||||
objects = TeacherManager()
|
objects = TeacherManager()
|
||||||
|
objects_custom = TeacherQuerySet.as_manager()
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return "%s (%s)" % (self.name, ", ".join(q.name for q in self.qualifications.all()))
|
return "%s (%s)" % (self.name, ", ".join(q.name for q in self.qualifications.all()))
|
||||||
|
|
|
@ -15,8 +15,8 @@ from django.utils.encoding import force_text
|
||||||
from .models import (
|
from .models import (
|
||||||
Author, Author2, AuthorAddress, AuthorWithAge, Bio, Book, Bookmark,
|
Author, Author2, AuthorAddress, AuthorWithAge, Bio, Book, Bookmark,
|
||||||
BookReview, BookWithYear, Comment, Department, Employee, FavoriteAuthors,
|
BookReview, BookWithYear, Comment, Department, Employee, FavoriteAuthors,
|
||||||
House, LessonEntry, Person, Qualification, Reader, Room, TaggedItem,
|
House, LessonEntry, ModelIterableSubclass, Person, Qualification, Reader,
|
||||||
Teacher, WordEntry,
|
Room, TaggedItem, Teacher, WordEntry,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -738,6 +738,9 @@ class CustomPrefetchTests(TestCase):
|
||||||
def test_values_queryset(self):
|
def test_values_queryset(self):
|
||||||
with self.assertRaisesMessage(ValueError, 'Prefetch querysets cannot use values().'):
|
with self.assertRaisesMessage(ValueError, 'Prefetch querysets cannot use values().'):
|
||||||
Prefetch('houses', House.objects.values('pk'))
|
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):
|
def test_to_attr_doesnt_cache_through_attr_as_list(self):
|
||||||
house = House.objects.prefetch_related(
|
house = House.objects.prefetch_related(
|
||||||
|
|
Loading…
Reference in New Issue