1
0
mirror of https://github.com/django/django.git synced 2025-10-24 06:06:09 +00:00

Refs #33651 -- Removed Prefetch.get_current_queryset() and get_prefetch_queryset() per deprecation timeline.

This commit is contained in:
Sarah Boyce
2024-12-12 18:12:23 +01:00
parent 55f71b195b
commit 817bc5800b
9 changed files with 21 additions and 252 deletions

View File

@@ -4,18 +4,15 @@ from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import ObjectDoesNotExist
from django.db import NotSupportedError, connection
from django.db.models import Prefetch, QuerySet, prefetch_related_objects
from django.db.models.fields.related import ForwardManyToOneDescriptor
from django.db.models.query import get_prefetcher, prefetch_one_level
from django.db.models.query import get_prefetcher
from django.db.models.sql import Query
from django.test import (
TestCase,
ignore_warnings,
override_settings,
skipIfDBFeature,
skipUnlessDBFeature,
)
from django.test.utils import CaptureQueriesContext
from django.utils.deprecation import RemovedInDjango60Warning
from .models import (
Article,
@@ -2013,59 +2010,3 @@ class PrefetchLimitTests(TestDataMixin, TestCase):
with self.subTest(book=book):
self.assertEqual(len(book.authors_sliced), 1)
self.assertIn(book.authors_sliced[0], list(book.authors.all()))
class DeprecationTests(TestCase):
def test_get_current_queryset_warning(self):
msg = (
"Prefetch.get_current_queryset() is deprecated. Use "
"get_current_querysets() instead."
)
authors = Author.objects.all()
with self.assertWarnsMessage(RemovedInDjango60Warning, msg) as ctx:
self.assertEqual(
Prefetch("authors", authors).get_current_queryset(1),
authors,
)
self.assertEqual(ctx.filename, __file__)
with self.assertWarnsMessage(RemovedInDjango60Warning, msg) as ctx:
self.assertIsNone(Prefetch("authors").get_current_queryset(1))
self.assertEqual(ctx.filename, __file__)
@ignore_warnings(category=RemovedInDjango60Warning)
def test_prefetch_one_level_fallback(self):
class NoGetPrefetchQuerySetsDescriptor(ForwardManyToOneDescriptor):
def get_prefetch_queryset(self, instances, queryset=None):
if queryset is None:
return super().get_prefetch_querysets(instances)
return super().get_prefetch_querysets(instances, [queryset])
def __getattribute__(self, name):
if name == "get_prefetch_querysets":
raise AttributeError
return super().__getattribute__(name)
house = House.objects.create()
room = Room.objects.create(house=house)
house.main_room = room
house.save()
# prefetch_one_level() fallbacks to get_prefetch_queryset().
prefetcher = NoGetPrefetchQuerySetsDescriptor(Room._meta.get_field("house"))
obj_list, additional_lookups = prefetch_one_level(
[room],
prefetcher,
Prefetch("house", House.objects.all()),
0,
)
self.assertEqual(obj_list, [house])
self.assertEqual(additional_lookups, [])
obj_list, additional_lookups = prefetch_one_level(
[room],
prefetcher,
Prefetch("house"),
0,
)
self.assertEqual(obj_list, [house])
self.assertEqual(additional_lookups, [])