summaryrefslogtreecommitdiff
path: root/tests/prefetch_related
diff options
context:
space:
mode:
authorMartin Svoboda <martin.svoboda@gmail.com>2021-08-11 16:42:43 +0200
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2021-10-14 12:37:03 +0200
commitcc4cb95beff0b75ec169add7e94cc481624a41e6 (patch)
tree9abb66f50321373edf513ccae36698577d56fe65 /tests/prefetch_related
parentcdad96e6330cd31185f7496aaf8eb316f2773d6d (diff)
downloaddjango-cc4cb95beff0b75ec169add7e94cc481624a41e6.tar.gz
Fixed #33008 -- Fixed prefetch_related() for deleted GenericForeignKeys.
Thanks Simon Charette for the implementation idea.
Diffstat (limited to 'tests/prefetch_related')
-rw-r--r--tests/prefetch_related/tests.py18
1 files changed, 18 insertions, 0 deletions
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):