Skip to content

Commit d009420

Browse files
authored
Merge pull request #1569 from ziima/pagination-items-attribute
Fix custom items_attribute in pagination
2 parents 0de9885 + c2188e4 commit d009420

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

ninja/pagination.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def paginate_queryset(
100100
offset = pagination.offset
101101
limit: int = min(pagination.limit, settings.PAGINATION_MAX_LIMIT)
102102
return {
103-
"items": queryset[offset : offset + limit],
103+
self.items_attribute: queryset[offset : offset + limit],
104104
"count": self._items_count(queryset),
105105
} # noqa: E203
106106

@@ -118,7 +118,7 @@ async def apaginate_queryset(
118118
else:
119119
items = queryset[offset : offset + limit]
120120
return {
121-
"items": items,
121+
self.items_attribute: items,
122122
"count": await self._aitems_count(queryset),
123123
} # noqa: E203
124124

@@ -154,7 +154,7 @@ def paginate_queryset(
154154
page_size = self._get_page_size(pagination.page_size)
155155
offset = (pagination.page - 1) * page_size
156156
return {
157-
"items": queryset[offset : offset + page_size],
157+
self.items_attribute: queryset[offset : offset + page_size],
158158
"count": self._items_count(queryset),
159159
} # noqa: E203
160160

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

176176
return {
177-
"items": items,
177+
self.items_attribute: items,
178178
"count": await self._aitems_count(queryset),
179179
} # noqa: E203
180180

tests/test_pagination.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,26 @@ def paginate_queryset(self, items, pagination: Input, request, **params):
9999
}
100100

101101

102+
class CustomItemsLimitOffsetPagination(LimitOffsetPagination):
103+
"""Minimal LimitOffsetPagination with custom items_attribute."""
104+
105+
items_attribute = "results"
106+
107+
class Output(Schema):
108+
results: List[int]
109+
count: int
110+
111+
112+
class CustomItemsPageNumberPagination(PageNumberPagination):
113+
"""Minimal PageNumberPagination with custom items_attribute."""
114+
115+
items_attribute = "results"
116+
117+
class Output(Schema):
118+
results: List[int]
119+
count: int
120+
121+
102122
class NoPagination(PaginationBase):
103123
"""
104124
Pagination class that returns all records without slicing.
@@ -174,6 +194,18 @@ def items_10(request):
174194
return ITEMS
175195

176196

197+
@api.get("/items_11", response=List[int])
198+
@paginate(CustomItemsLimitOffsetPagination)
199+
def items_11(request):
200+
return list(range(100))
201+
202+
203+
@api.get("/items_12", response=List[int])
204+
@paginate(CustomItemsPageNumberPagination)
205+
def items_12(request):
206+
return list(range(100))
207+
208+
177209
@api.get("/items_no_pagination", response=List[int])
178210
@paginate(NoPagination)
179211
def items_no_pagination(request):
@@ -583,6 +615,16 @@ def items_11(request, **kwargs):
583615
}
584616

585617

618+
def test_case11():
619+
response = client.get("/items_11").json()
620+
assert response == {"results": list(range(100)), "count": 100}
621+
622+
623+
def test_case12():
624+
response = client.get("/items_12").json()
625+
assert response == {"results": list(range(100)), "count": 100}
626+
627+
586628
def test_config_error_None():
587629
with pytest.raises(ConfigError):
588630

0 commit comments

Comments
 (0)