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

Fixed #36432 -- Fixed a prefetch_related crash on related target subclass queryset.

Regression in 626d77e52a3f247358514bcf51c761283968099c.

Refs #36116.

Thanks Cornelis Poppema for the excellent report.
This commit is contained in:
Simon Charette 2025-06-03 22:34:39 -04:00 committed by Sarah Boyce
parent c075508b4d
commit 08187c94ed
4 changed files with 25 additions and 1 deletions

View File

@ -169,8 +169,11 @@ class ForwardManyToOneDescriptor:
rel_obj_attr = self.field.get_foreign_related_value
instance_attr = self.field.get_local_related_value
instances_dict = {instance_attr(inst): inst for inst in instances}
related_fields = self.field.foreign_related_fields
remote_field = self.field.remote_field
related_fields = [
queryset.query.resolve_ref(field.name).target
for field in self.field.foreign_related_fields
]
queryset = queryset.filter(
TupleIn(
ColPairs(

View File

@ -43,3 +43,7 @@ Bugfixes
<django.http.HttpRequest.get_preferred_type>` did not account for media type
parameters in ``Accept`` headers, reducing specificity in content negotiation
(:ticket:`36411`).
* Fixed a regression in Django 5.2 that caused a crash when using
``QuerySet.prefetch_related()`` to prefetch a foreign key with a ``Prefetch``
queryset for a subclass of the foreign target (:ticket:`36432`).

View File

@ -280,6 +280,10 @@ class Employee(models.Model):
ordering = ["id"]
class SelfDirectedEmployee(Employee):
pass
# Ticket #19607

View File

@ -37,6 +37,7 @@ from .models import (
Qualification,
Reader,
Room,
SelfDirectedEmployee,
TaggedItem,
Teacher,
WordEntry,
@ -433,6 +434,18 @@ class PrefetchRelatedTests(TestDataMixin, TestCase):
authors[1].active_favorite_authors, [self.author3, self.author4]
)
def test_prefetch_queryset_child_class(self):
employee = SelfDirectedEmployee.objects.create(name="Foo")
employee.boss = employee
employee.save()
with self.assertNumQueries(2):
retrieved_employee = SelfDirectedEmployee.objects.prefetch_related(
Prefetch("boss", SelfDirectedEmployee.objects.all())
).get()
with self.assertNumQueries(0):
self.assertEqual(retrieved_employee, employee)
self.assertEqual(retrieved_employee.boss, retrieved_employee)
class RawQuerySetTests(TestDataMixin, TestCase):
def test_basic(self):