1
0
mirror of https://github.com/django/django.git synced 2024-12-26 19:16:11 +00:00

[4.2.x] Refs #30052 -- Clarified that defer() and only() do not work with aggregated fields.

Backport of b126f69416 from main
This commit is contained in:
Vyacheslav Dmitriev 2023-07-20 20:02:17 +03:00 committed by Natalia
parent 7a67b065d7
commit da92a971a0

View File

@ -1723,6 +1723,11 @@ never defer the loading of the primary key. If you are using
loading of the field that connects from the primary model to the related loading of the field that connects from the primary model to the related
one, doing so will result in an error. one, doing so will result in an error.
Similarly, calling ``defer()`` (or its counterpart :meth:`only()`) including an
argument from an aggregation (e.g. using the result of :meth:`annotate()`)
doesn't make sense: doing so will raise an exception. The aggregated values
will always be fetched into the resulting queryset.
.. note:: .. note::
The ``defer()`` method (and its cousin, :meth:`only()`, below) are only for The ``defer()`` method (and its cousin, :meth:`only()`, below) are only for
@ -1817,8 +1822,9 @@ All of the cautions in the note for the :meth:`defer` documentation apply to
``only()`` as well. Use it cautiously and only after exhausting your other ``only()`` as well. Use it cautiously and only after exhausting your other
options. options.
Using :meth:`only` and omitting a field requested using :meth:`select_related` Using ``only()`` and omitting a field requested using :meth:`select_related` is
is an error as well. an error as well. On the other hand, invoking ``only()`` without any arguments,
will return every field (including annotations) fetched by the queryset.
As with ``defer()``, you cannot access the non-loaded fields from asynchronous As with ``defer()``, you cannot access the non-loaded fields from asynchronous
code and expect them to load. Instead, you will get a code and expect them to load. Instead, you will get a