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,
)
| 2574 | @_generative |
| 2575 | @_assertions(_no_statement_condition) |
| 2576 | def slice( |
| 2577 | self, |
| 2578 | start: int, |
| 2579 | stop: int, |
| 2580 | ) -> Self: |
| 2581 | """Computes the "slice" of the :class:`_query.Query` represented by |
| 2582 | the given indices and returns the resulting :class:`_query.Query`. |
| 2583 | |
| 2584 | The start and stop indices behave like the argument to Python's |
| 2585 | built-in :func:`range` function. This method provides an |
| 2586 | alternative to using ``LIMIT``/``OFFSET`` to get a slice of the |
| 2587 | query. |
| 2588 | |
| 2589 | For example, :: |
| 2590 | |
| 2591 | session.query(User).order_by(User.id).slice(1, 3) |
| 2592 | |
| 2593 | renders as |
| 2594 | |
| 2595 | .. sourcecode:: sql |
| 2596 | |
| 2597 | SELECT users.id AS users_id, |
| 2598 | users.name AS users_name |
| 2599 | FROM users ORDER BY users.id |
| 2600 | LIMIT ? OFFSET ? |
| 2601 | (2, 1) |
| 2602 | |
| 2603 | .. seealso:: |
| 2604 | |
| 2605 | :meth:`_query.Query.limit` |
| 2606 | |
| 2607 | :meth:`_query.Query.offset` |
| 2608 | |
| 2609 | :meth:`_sql.Select.slice` - v2 equivalent method. |
| 2610 | |
| 2611 | """ |
| 2612 | |
| 2613 | self._limit_clause, self._offset_clause = sql_util._make_slice( |
| 2614 | self._limit_clause, self._offset_clause, start, stop |
| 2615 | ) |
| 2616 | return self |
| 2617 | |
| 2618 | @_generative |
| 2619 | @_assertions(_no_statement_condition) |
no outgoing calls