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 /docs | |
| 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 'docs')
| -rw-r--r-- | docs/internals/deprecation.txt | 8 | ||||
| -rw-r--r-- | docs/ref/contrib/contenttypes.txt | 26 | ||||
| -rw-r--r-- | docs/ref/models/querysets.txt | 13 | ||||
| -rw-r--r-- | docs/releases/5.0.txt | 12 |
4 files changed, 54 insertions, 5 deletions
diff --git a/docs/internals/deprecation.txt b/docs/internals/deprecation.txt index a400ed97f9..632c1e219d 100644 --- a/docs/internals/deprecation.txt +++ b/docs/internals/deprecation.txt @@ -45,6 +45,14 @@ details on these changes. * The ``ChoicesMeta`` alias to ``django.db.models.enums.ChoicesType`` will be removed. +* The ``Prefetch.get_current_queryset()`` method will be removed. + +* The ``get_prefetch_queryset()`` method of related managers and descriptors + will be removed. + +* ``get_prefetcher()`` and ``prefetch_related_objects()`` will no longer + fallback to ``get_prefetch_queryset()``. + .. _deprecation-removed-in-5.1: 5.1 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:: diff --git a/docs/releases/5.0.txt b/docs/releases/5.0.txt index 6ba9a7a92b..8f8814271a 100644 --- a/docs/releases/5.0.txt +++ b/docs/releases/5.0.txt @@ -243,7 +243,9 @@ Minor features :mod:`django.contrib.contenttypes` ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -* ... +* :meth:`.QuerySet.prefetch_related` now supports prefetching + :class:`~django.contrib.contenttypes.fields.GenericForeignKey` with + non-homogeneous set of results. :mod:`django.contrib.gis` ~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -711,6 +713,14 @@ Miscellaneous * The ``django.db.models.enums.ChoicesMeta`` metaclass is renamed to ``ChoicesType``. +* The ``Prefetch.get_current_queryset()`` method is deprecated. + +* The ``get_prefetch_queryset()`` method of related managers and descriptors + is deprecated. Starting with Django 6.0, ``get_prefetcher()`` and + ``prefetch_related_objects()`` will no longer fallback to + ``get_prefetch_queryset()``. Subclasses should implement + ``get_prefetch_querysets()`` instead. + .. _`oracledb`: https://oracle.github.io/python-oracledb/ Features removed in 5.0 |
