Skip to content

Commit eeb9d63

Browse files
committed
Fix custom items_attribute in pagination
1 parent ecc3f98 commit eeb9d63

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
@@ -97,7 +97,7 @@ def paginate_queryset(
9797
offset = pagination.offset
9898
limit: int = min(pagination.limit, settings.PAGINATION_MAX_LIMIT)
9999
return {
100-
"items": queryset[offset : offset + limit],
100+
self.items_attribute: queryset[offset : offset + limit],
101101
"count": self._items_count(queryset),
102102
} # noqa: E203
103103

@@ -114,7 +114,7 @@ async def apaginate_queryset(
114114
else:
115115
items = queryset[offset : offset + limit]
116116
return {
117-
"items": items,
117+
self.items_attribute: items,
118118
"count": await self._aitems_count(queryset),
119119
} # noqa: E203
120120

@@ -149,7 +149,7 @@ def paginate_queryset(
149149
page_size = self._get_page_size(pagination.page_size)
150150
offset = (pagination.page - 1) * page_size
151151
return {
152-
"items": queryset[offset : offset + page_size],
152+
self.items_attribute: queryset[offset : offset + page_size],
153153
"count": self._items_count(queryset),
154154
} # noqa: E203
155155

@@ -168,7 +168,7 @@ async def apaginate_queryset(
168168
items = queryset[offset : offset + page_size]
169169

170170
return {
171-
"items": items,
171+
self.items_attribute: items,
172172
"count": await self._aitems_count(queryset),
173173
} # noqa: E203
174174

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
@api.get("/items_1", response=List[int])
103123
@paginate # WITHOUT brackets (should use default pagination)
104124
def items_1(request, **kwargs):
@@ -160,6 +180,18 @@ def items_10(request):
160180
return ITEMS
161181

162182

183+
@api.get("/items_11", response=List[int])
184+
@paginate(CustomItemsLimitOffsetPagination)
185+
def items_11(request):
186+
return list(range(100))
187+
188+
189+
@api.get("/items_12", response=List[int])
190+
@paginate(CustomItemsPageNumberPagination)
191+
def items_12(request):
192+
return list(range(100))
193+
194+
163195
client = TestClient(api)
164196

165197

@@ -563,6 +595,16 @@ def items_11(request, **kwargs):
563595
}
564596

565597

598+
def test_case11():
599+
response = client.get("/items_11").json()
600+
assert response == {"results": list(range(100)), "count": 100}
601+
602+
603+
def test_case12():
604+
response = client.get("/items_12").json()
605+
assert response == {"results": list(range(100)), "count": 100}
606+
607+
566608
def test_config_error_None():
567609
with pytest.raises(ConfigError):
568610

0 commit comments

Comments
 (0)