summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorClaude Paroz <claude@2xlibre.net>2017-04-19 10:24:22 +0200
committerClaude Paroz <claude@2xlibre.net>2017-04-19 19:37:13 +0200
commit61e883c7c268f3460a85be1c66ea1773711cd6c7 (patch)
treedbecd119fdc40a1dd6efd99dfd2f128be04d48c3 /tests
parentcf1e682c8c68d7187c34f46f2717b22f830d92a1 (diff)
[1.11.x] Fixed #28096 -- Allowed prefetch calls with ModelIterable subclasses
Regression in 7ec330eeb96d0874949eacb8ed1bbb97e11807e1. Thanks Tim Graham for the review. Backport of 43b4a1618ed85151494d68778a1bb6304ce1cb79 from master.
Diffstat (limited to 'tests')
-rw-r--r--tests/prefetch_related/models.py12
-rw-r--r--tests/prefetch_related/tests.py7
2 files changed, 17 insertions, 2 deletions
diff --git a/tests/prefetch_related/models.py b/tests/prefetch_related/models.py
index 064ce1dfbd..6600418b8f 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.encoding import python_2_unicode_compatible
from django.utils.functional import cached_property
@@ -100,6 +101,16 @@ class Qualification(models.Model):
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):
def get_queryset(self):
return super(TeacherManager, self).get_queryset().prefetch_related('qualifications')
@@ -111,6 +122,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 7ddf4a4c96..23ea2458fc 100644
--- a/tests/prefetch_related/tests.py
+++ b/tests/prefetch_related/tests.py
@@ -15,8 +15,8 @@ from django.utils.encoding import force_text
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,
)
@@ -738,6 +738,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(