11from __future__ import annotations
22
33import types
4- from collections .abc import AsyncIterator , Callable , Generator , Iterable
4+ from collections .abc import AsyncIterator , Callable , Collection , Generator , Iterable
55from copy import copy
66from typing import TYPE_CHECKING , Any , Generic , Optional , TypeVar , cast , overload
77
88from pypika_tortoise import JoinType , Order , Table
99from pypika_tortoise .analytics import Count
1010from pypika_tortoise .functions import Cast
1111from pypika_tortoise .queries import QueryBuilder
12- from pypika_tortoise .terms import Case , Field , Star , Term , ValueWrapper
12+ from pypika_tortoise .terms import Case , Field , Star , Term , ValueWrapper , PseudoColumn
1313from typing_extensions import Literal , Protocol
1414
1515from tortoise .backends .base .client import BaseDBAsyncClient , Capabilities
@@ -179,7 +179,8 @@ def resolve_ordering(
179179 model : type [Model ],
180180 table : Table ,
181181 orderings : Iterable [tuple [str , str | Order ]],
182- annotations : dict [str , Any ],
182+ annotations : dict [str , Term | Expression ],
183+ fields_for_select : Collection [str ] | None = None ,
183184 ) -> None :
184185 """
185186 Applies standard ordering to QuerySet.
@@ -189,6 +190,8 @@ def resolve_ordering(
189190 (to allow self referential joins)
190191 :param orderings: What columns/order to order by
191192 :param annotations: Annotations that may be ordered on
193+ :param fields_for_select: Contains fields that are selected in the SELECT clause if
194+ .only(), .values() or .values_list() are used.
192195
193196 :raises FieldError: If a field provided does not exist in model.
194197 """
@@ -214,18 +217,27 @@ def resolve_ordering(
214217 {},
215218 )
216219 elif field_name in annotations :
217- if isinstance (annotation := annotations [field_name ], Term ):
218- term : Term = annotation
220+ term : Term
221+ if not fields_for_select or field_name in fields_for_select :
222+ # The annotation is SELECTed, we can just reference it in the following cases:
223+ # - Empty fields_for_select means that all columns and annotations are selected,
224+ # hence we can reference the annotation.
225+ # - The annotation is in fields_for_select, hence we can reference it.
226+ term = PseudoColumn (field_name )
219227 else :
220- annotation_info = annotation .resolve (
221- ResolveContext (
222- model = self .model ,
223- table = table ,
224- annotations = annotations ,
225- custom_filters = {},
226- )
227- )
228- term = annotation_info .term
228+ # The annotation is not in SELECT, resolve it
229+ annotation = annotations [field_name ]
230+ if isinstance (annotation , Term ):
231+ term = annotation
232+ else :
233+ term = annotation .resolve (
234+ ResolveContext (
235+ model = self .model ,
236+ table = table ,
237+ annotations = annotations ,
238+ custom_filters = {},
239+ )
240+ ).term
229241 self .query = self .query .orderby (term , order = ordering [1 ])
230242 else :
231243 field_object = model ._meta .fields_map .get (field_name )
@@ -1078,7 +1090,11 @@ def _make_query(self) -> None:
10781090 if append_item not in self ._select_related_idx :
10791091 self ._select_related_idx .append (append_item )
10801092 self .resolve_ordering (
1081- self .model , self .model ._meta .basetable , self ._orderings , self ._annotations
1093+ self .model ,
1094+ self .model ._meta .basetable ,
1095+ self ._orderings ,
1096+ self ._annotations ,
1097+ self ._fields_for_select ,
10821098 )
10831099 self .resolve_filters ()
10841100 if self ._limit is not None :
@@ -1562,6 +1578,7 @@ def _make_query(self) -> None:
15621578 table = self .model ._meta .basetable ,
15631579 orderings = self ._orderings ,
15641580 annotations = self ._annotations ,
1581+ fields_for_select = self ._fields_for_select_list ,
15651582 )
15661583 self .resolve_filters ()
15671584 if self ._limit :
@@ -1683,6 +1700,7 @@ def _make_query(self) -> None:
16831700 table = self .model ._meta .basetable ,
16841701 orderings = self ._orderings ,
16851702 annotations = self ._annotations ,
1703+ fields_for_select = self ._fields_for_select .keys (),
16861704 )
16871705 self .resolve_filters ()
16881706
0 commit comments