Apply one or more ORDER BY criteria to the query and return the newly resulting :class:`_query.Query`. e.g.:: q = session.query(Entity).order_by(Entity.id, Entity.name) Calling this method multiple times is equivalent to calling it once with all the cla
(
self,
__first: Union[
Literal[None, False, _NoArg.NO_ARG],
_ColumnExpressionOrStrLabelArgument[Any],
] = _NoArg.NO_ARG,
/,
*clauses: _ColumnExpressionOrStrLabelArgument[Any],
)
| 2025 | |
| 2026 | @_generative |
| 2027 | def order_by( |
| 2028 | self, |
| 2029 | __first: Union[ |
| 2030 | Literal[None, False, _NoArg.NO_ARG], |
| 2031 | _ColumnExpressionOrStrLabelArgument[Any], |
| 2032 | ] = _NoArg.NO_ARG, |
| 2033 | /, |
| 2034 | *clauses: _ColumnExpressionOrStrLabelArgument[Any], |
| 2035 | ) -> Self: |
| 2036 | class="st">"""Apply one or more ORDER BY criteria to the query and return |
| 2037 | the newly resulting :class:`_query.Query`. |
| 2038 | |
| 2039 | e.g.:: |
| 2040 | |
| 2041 | q = session.query(Entity).order_by(Entity.id, Entity.name) |
| 2042 | |
| 2043 | Calling this method multiple times is equivalent to calling it once |
| 2044 | with all the clauses concatenated. All existing ORDER BY criteria may |
| 2045 | be cancelled by passing ``None`` by itself. New ORDER BY criteria may |
| 2046 | then be added by invoking :meth:`_orm.Query.order_by` again, e.g.:: |
| 2047 | |
| 2048 | class="cm"># will erase all ORDER BY and ORDER BY new_col alone |
| 2049 | q = q.order_by(None).order_by(new_col) |
| 2050 | |
| 2051 | .. seealso:: |
| 2052 | |
| 2053 | These sections describe ORDER BY in terms of :term:`2.0 style` |
| 2054 | invocation but apply to :class:`_orm.Query` as well: |
| 2055 | |
| 2056 | :ref:`tutorial_order_by` - in the :ref:`unified_tutorial` |
| 2057 | |
| 2058 | :ref:`tutorial_order_by_label` - in the :ref:`unified_tutorial` |
| 2059 | |
| 2060 | :meth:`_sql.Select.order_by` - v2 equivalent method. |
| 2061 | |
| 2062 | class="st">""" |
| 2063 | |
| 2064 | for assertion in (self._no_statement_condition, self._no_limit_offset): |
| 2065 | assertion(class="st">"order_by") |
| 2066 | |
| 2067 | if not clauses and (__first is None or __first is False): |
| 2068 | self._order_by_clauses = () |
| 2069 | elif __first is not _NoArg.NO_ARG: |
| 2070 | criterion = tuple( |
| 2071 | coercions.expect(roles.OrderByRole, clause) |
| 2072 | for clause in (__first,) + clauses |
| 2073 | ) |
| 2074 | self._order_by_clauses += criterion |
| 2075 | |
| 2076 | return self |
| 2077 | |
| 2078 | @_generative |
| 2079 | def group_by( |
no outgoing calls