From cc4cb95beff0b75ec169add7e94cc481624a41e6 Mon Sep 17 00:00:00 2001 From: Martin Svoboda Date: Wed, 11 Aug 2021 16:42:43 +0200 Subject: Fixed #33008 -- Fixed prefetch_related() for deleted GenericForeignKeys. Thanks Simon Charette for the implementation idea. --- tests/prefetch_related/tests.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'tests/prefetch_related/tests.py') diff --git a/tests/prefetch_related/tests.py b/tests/prefetch_related/tests.py index cba1897fc5..242ff6c366 100644 --- a/tests/prefetch_related/tests.py +++ b/tests/prefetch_related/tests.py @@ -1033,6 +1033,24 @@ class GenericRelationTests(TestCase): # instance returned by the manager. self.assertEqual(list(bookmark.tags.all()), list(bookmark.tags.all().all())) + def test_deleted_GFK(self): + TaggedItem.objects.create(tag='awesome', content_object=self.book1) + TaggedItem.objects.create(tag='awesome', content_object=self.book2) + ct = ContentType.objects.get_for_model(Book) + + book1_pk = self.book1.pk + self.book1.delete() + + with self.assertNumQueries(2): + qs = TaggedItem.objects.filter(tag='awesome').prefetch_related('content_object') + result = [ + (tag.object_id, tag.content_type_id, tag.content_object) for tag in qs + ] + self.assertEqual(result, [ + (book1_pk, ct.pk, None), + (self.book2.pk, ct.pk, self.book2), + ]) + class MultiTableInheritanceTest(TestCase): -- cgit v1.2.1