summaryrefslogtreecommitdiff
path: root/docs/ref
diff options
context:
space:
mode:
authorClément Escolano <clement.escolano@icloud.com>2023-08-01 23:31:40 +0200
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2023-09-18 13:23:21 +0200
commitcac94dd8aa2fb49cd2e06b5b37cf039257284bb0 (patch)
tree5dda5f6607c0b3fa2cac9595f7b133aaa04b504d /docs/ref
parent190874eadd0c6dcaae0c244cc47e838cf0faf24d (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 'docs/ref')
-rw-r--r--docs/ref/contrib/contenttypes.txt26
-rw-r--r--docs/ref/models/querysets.txt13
2 files changed, 35 insertions, 4 deletions
diff --git a/docs/ref/contrib/contenttypes.txt b/docs/ref/contrib/contenttypes.txt
index c2e448329e..023db0b672 100644
--- a/docs/ref/contrib/contenttypes.txt
+++ b/docs/ref/contrib/contenttypes.txt
@@ -590,3 +590,29 @@ information.
Subclasses of :class:`GenericInlineModelAdmin` with stacked and tabular
layouts, respectively.
+
+.. module:: django.contrib.contenttypes.prefetch
+
+``GenericPrefetch()``
+---------------------
+
+.. versionadded:: 5.0
+
+.. class:: GenericPrefetch(lookup, querysets=None, to_attr=None)
+
+This lookup is similar to ``Prefetch()`` and it should only be used on
+``GenericForeignKey``. The ``querysets`` argument accepts a list of querysets,
+each for a different ``ContentType``. This is useful for ``GenericForeignKey``
+with non-homogeneous set of results.
+
+.. code-block:: pycon
+
+ >>> bookmark = Bookmark.objects.create(url="https://www.djangoproject.com/")
+ >>> animal = Animal.objects.create(name="lion", weight=100)
+ >>> TaggedItem.objects.create(tag="great", content_object=bookmark)
+ >>> TaggedItem.objects.create(tag="awesome", content_object=animal)
+ >>> prefetch = GenericPrefetch(
+ ... "content_object", [Bookmark.objects.all(), Animal.objects.only("name")]
+ ... )
+ >>> TaggedItem.objects.prefetch_related(prefetch).all()
+ <QuerySet [<TaggedItem: Great>, <TaggedItem: Awesome>]>
diff --git a/docs/ref/models/querysets.txt b/docs/ref/models/querysets.txt
index 9d90f4b19e..cd6c13fc05 100644
--- a/docs/ref/models/querysets.txt
+++ b/docs/ref/models/querysets.txt
@@ -1154,10 +1154,15 @@ many-to-many, many-to-one, and
cannot be done using ``select_related``, in addition to the foreign key and
one-to-one relationships that are supported by ``select_related``. It also
supports prefetching of
-:class:`~django.contrib.contenttypes.fields.GenericForeignKey`, however, it
-must be restricted to a homogeneous set of results. For example, prefetching
-objects referenced by a ``GenericForeignKey`` is only supported if the query
-is restricted to one ``ContentType``.
+:class:`~django.contrib.contenttypes.fields.GenericForeignKey`, however, the
+queryset for each ``ContentType`` must be provided in the ``querysets``
+parameter of :class:`~django.contrib.contenttypes.prefetch.GenericPrefetch`.
+
+.. versionchanged:: 5.0
+
+ Support for prefetching
+ :class:`~django.contrib.contenttypes.fields.GenericForeignKey` with
+ non-homogeneous set of results was added.
For example, suppose you have these models::