diff options
| author | Clément Escolano <clement.escolano@icloud.com> | 2023-08-01 23:31:40 +0200 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2023-09-18 13:23:21 +0200 |
| commit | cac94dd8aa2fb49cd2e06b5b37cf039257284bb0 (patch) | |
| tree | 5dda5f6607c0b3fa2cac9595f7b133aaa04b504d /tests/generic_relations | |
| parent | 190874eadd0c6dcaae0c244cc47e838cf0faf24d (diff) | |
Fixed #33651 -- Added support for prefetching GenericForeignKey.
Co-authored-by: revanthgss <revanthgss@almabase.com>
Co-authored-by: Mariusz Felisiak <felisiak.mariusz@gmail.com>
Diffstat (limited to 'tests/generic_relations')
| -rw-r--r-- | tests/generic_relations/tests.py | 35 |
1 files changed, 34 insertions, 1 deletions
diff --git a/tests/generic_relations/tests.py b/tests/generic_relations/tests.py index fab23dfde5..e0c6fe2db7 100644 --- a/tests/generic_relations/tests.py +++ b/tests/generic_relations/tests.py @@ -1,6 +1,7 @@ from django.contrib.contenttypes.models import ContentType +from django.contrib.contenttypes.prefetch import GenericPrefetch from django.core.exceptions import FieldError -from django.db.models import Q +from django.db.models import Q, prefetch_related_objects from django.test import SimpleTestCase, TestCase, skipUnlessDBFeature from .models import ( @@ -747,6 +748,38 @@ class GenericRelationsTests(TestCase): comparison.first_obj.comparisons.all(), [comparison] ) + def test_generic_prefetch(self): + tagged_vegetable = TaggedItem.objects.create( + tag="great", content_object=self.bacon + ) + tagged_animal = TaggedItem.objects.create( + tag="awesome", content_object=self.platypus + ) + # Getting the instances again so that content object is deferred. + tagged_vegetable = TaggedItem.objects.get(pk=tagged_vegetable.pk) + tagged_animal = TaggedItem.objects.get(pk=tagged_animal.pk) + + with self.assertNumQueries(2): + prefetch_related_objects( + [tagged_vegetable, tagged_animal], + GenericPrefetch( + "content_object", + [Vegetable.objects.all(), Animal.objects.only("common_name")], + ), + ) + with self.assertNumQueries(0): + self.assertEqual(tagged_vegetable.content_object.name, self.bacon.name) + with self.assertNumQueries(0): + self.assertEqual( + tagged_animal.content_object.common_name, + self.platypus.common_name, + ) + with self.assertNumQueries(1): + self.assertEqual( + tagged_animal.content_object.latin_name, + self.platypus.latin_name, + ) + class ProxyRelatedModelTest(TestCase): def test_default_behavior(self): |
