mirror of
https://github.com/django/django.git
synced 2025-10-23 21:59:11 +00:00
Fixed #30493 -- Fixed prefetch_related() for GenericRelation with different content types.
Co-Authored-By: Mariusz Felisiak <felisiak.mariusz@gmail.com> Thanks Simon Charette for the review.
This commit is contained in:
@@ -546,6 +546,24 @@ class GenericRelationsTests(TestCase):
|
||||
platypus.tags.remove(weird_tag)
|
||||
self.assertSequenceEqual(platypus.tags.all(), [furry_tag])
|
||||
|
||||
def test_prefetch_related_different_content_types(self):
|
||||
TaggedItem.objects.create(content_object=self.platypus, tag='prefetch_tag_1')
|
||||
TaggedItem.objects.create(
|
||||
content_object=Vegetable.objects.create(name='Broccoli'),
|
||||
tag='prefetch_tag_2',
|
||||
)
|
||||
TaggedItem.objects.create(
|
||||
content_object=Animal.objects.create(common_name='Bear'),
|
||||
tag='prefetch_tag_3',
|
||||
)
|
||||
qs = TaggedItem.objects.filter(
|
||||
tag__startswith='prefetch_tag_',
|
||||
).prefetch_related('content_object', 'content_object__tags')
|
||||
with self.assertNumQueries(4):
|
||||
tags = list(qs)
|
||||
for tag in tags:
|
||||
self.assertSequenceEqual(tag.content_object.tags.all(), [tag])
|
||||
|
||||
|
||||
class ProxyRelatedModelTest(TestCase):
|
||||
def test_default_behavior(self):
|
||||
|
||||
Reference in New Issue
Block a user