Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions ninja/pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def paginate_queryset(
offset = pagination.offset
limit: int = min(pagination.limit, settings.PAGINATION_MAX_LIMIT)
return {
"items": queryset[offset : offset + limit],
self.items_attribute: queryset[offset : offset + limit],
"count": self._items_count(queryset),
} # noqa: E203

Expand All @@ -118,7 +118,7 @@ async def apaginate_queryset(
else:
items = queryset[offset : offset + limit]
return {
"items": items,
self.items_attribute: items,
"count": await self._aitems_count(queryset),
} # noqa: E203

Expand Down Expand Up @@ -154,7 +154,7 @@ def paginate_queryset(
page_size = self._get_page_size(pagination.page_size)
offset = (pagination.page - 1) * page_size
return {
"items": queryset[offset : offset + page_size],
self.items_attribute: queryset[offset : offset + page_size],
"count": self._items_count(queryset),
} # noqa: E203

Expand All @@ -174,7 +174,7 @@ async def apaginate_queryset(
items = queryset[offset : offset + page_size]

return {
"items": items,
self.items_attribute: items,
"count": await self._aitems_count(queryset),
} # noqa: E203

Expand Down
42 changes: 42 additions & 0 deletions tests/test_pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,26 @@ def paginate_queryset(self, items, pagination: Input, request, **params):
}


class CustomItemsLimitOffsetPagination(LimitOffsetPagination):
"""Minimal LimitOffsetPagination with custom items_attribute."""

items_attribute = "results"

class Output(Schema):
results: List[int]
count: int


class CustomItemsPageNumberPagination(PageNumberPagination):
"""Minimal PageNumberPagination with custom items_attribute."""

items_attribute = "results"

class Output(Schema):
results: List[int]
count: int


class NoPagination(PaginationBase):
"""
Pagination class that returns all records without slicing.
Expand Down Expand Up @@ -174,6 +194,18 @@ def items_10(request):
return ITEMS


@api.get("/items_11", response=List[int])
@paginate(CustomItemsLimitOffsetPagination)
def items_11(request):
return list(range(100))


@api.get("/items_12", response=List[int])
@paginate(CustomItemsPageNumberPagination)
def items_12(request):
return list(range(100))


@api.get("/items_no_pagination", response=List[int])
@paginate(NoPagination)
def items_no_pagination(request):
Expand Down Expand Up @@ -583,6 +615,16 @@ def items_11(request, **kwargs):
}


def test_case11():
response = client.get("/items_11").json()
assert response == {"results": list(range(100)), "count": 100}


def test_case12():
response = client.get("/items_12").json()
assert response == {"results": list(range(100)), "count": 100}


def test_config_error_None():
with pytest.raises(ConfigError):

Expand Down