Computes the "slice" of the :class:`_query.Query` represented by the given indices and returns the resulting :class:`_query.Query`. The start and stop indices behave like the argument to Python's built-in :func:`range` function. This method provides an alternative to
(
self,
start: int,
stop: int,
)
| 2604 | @_generative |
| 2605 | @_assertions(_no_statement_condition) |
| 2606 | def slice( |
| 2607 | self, |
| 2608 | start: int, |
| 2609 | stop: int, |
| 2610 | ) -> Self: |
| 2611 | """Computes the "slice" of the :class:`_query.Query` represented by |
| 2612 | the given indices and returns the resulting :class:`_query.Query`. |
| 2613 | |
| 2614 | The start and stop indices behave like the argument to Python's |
| 2615 | built-in :func:`range` function. This method provides an |
| 2616 | alternative to using ``LIMIT``/``OFFSET`` to get a slice of the |
| 2617 | query. |
| 2618 | |
| 2619 | For example, :: |
| 2620 | |
| 2621 | session.query(User).order_by(User.id).slice(1, 3) |
| 2622 | |
| 2623 | renders as |
| 2624 | |
| 2625 | .. sourcecode:: sql |
| 2626 | |
| 2627 | SELECT users.id AS users_id, |
| 2628 | users.name AS users_name |
| 2629 | FROM users ORDER BY users.id |
| 2630 | LIMIT ? OFFSET ? |
| 2631 | (2, 1) |
| 2632 | |
| 2633 | .. seealso:: |
| 2634 | |
| 2635 | :meth:`_query.Query.limit` |
| 2636 | |
| 2637 | :meth:`_query.Query.offset` |
| 2638 | |
| 2639 | :meth:`_sql.Select.slice` - v2 equivalent method. |
| 2640 | |
| 2641 | """ |
| 2642 | |
| 2643 | self._limit_clause, self._offset_clause = sql_util._make_slice( |
| 2644 | self._limit_clause, self._offset_clause, start, stop |
| 2645 | ) |
| 2646 | return self |
| 2647 | |
| 2648 | @_generative |
| 2649 | @_assertions(_no_statement_condition) |
no outgoing calls