MCPcopy Index your code
hub / github.com/ormar-orm/ormar / __getitem__

Method __getitem__

ormar/queryset/queryset.py:1499–1534  ·  view source on GitHub ↗

Returns a new ``QuerySet`` with LIMIT/OFFSET derived from a Python integer index or slice. Negative indices and negative slice bounds are translated into a reversed-order query plus an in-memory list reversal on ``.all()``, so that ``Model.objects[-N:]`` ret

(self, key: Union[int, slice])

Source from the content-addressed store, hash-verified

1497 )
1498
1499 def __getitem__(self, key: Union[int, slice]) -> "QuerySet[T]":
1500 """
1501 Returns a new ``QuerySet`` with LIMIT/OFFSET derived from a Python
1502 integer index or slice.
1503
1504 Negative indices and negative slice bounds are translated into a
1505 reversed-order query plus an in-memory list reversal on ``.all()``,
1506 so that ``Model.objects[-N:]`` returns the last ``N`` rows in the
1507 original ordering. Slice shapes that would require a ``COUNT(*)``
1508 round-trip (a bare ``[:-N]``, or mixed positive/negative bounds) are
1509 rejected with a ``QueryDefinitionError``; use ``.count()`` combined
1510 with ``.offset()``/``.limit()`` instead.
1511
1512 Each call replaces the previous pagination state rather than composing
1513 with it — subsequent slicing on an already-sliced queryset is not
1514 guaranteed to compose in a Python-list-like way.
1515
1516 :param key: integer index or slice
1517 :type key: int | slice
1518 :raises QueryDefinitionError: when ``key`` is not an ``int``/``slice``
1519 or when the slice cannot be expressed without a count
1520 :return: new QuerySet with updated pagination
1521 :rtype: QuerySet
1522 """
1523 bounds = normalize_slice(key)
1524 order_bys = self.order_bys
1525 if self._reverse_result != bounds.reverse:
1526 if not order_bys:
1527 order_bys = OrderAction.from_model_defaults(self.model)
1528 order_bys = [ob.flipped() for ob in order_bys]
1529 return self.rebuild_self(
1530 limit_count=bounds.limit,
1531 offset=bounds.offset,
1532 order_bys=order_bys,
1533 reverse_result=bounds.reverse,
1534 )

Callers

nothing calls this directly

Calls 4

rebuild_selfMethod · 0.95
normalize_sliceFunction · 0.90
from_model_defaultsMethod · 0.80
flippedMethod · 0.80

Tested by

no test coverage detected