You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
To create a custom pagination class you should subclass `ninja.pagination.PaginationBase` and override the `Input` and `Output` schema classes and `paginate_queryset(self, queryset, request, **params)` method:
102
97
103
-
- The `Input` schema is a Schema class that describes parameters that should be passed to your paginator (f.e. page-number or limit/offset values).
104
-
- The `Output` schema describes schema for page output (f.e. count/next-page/items/etc).
105
-
- The `paginate_queryset` method is passed the initial queryset and should return an iterable object that contains only the data in the requested page. This method accepts the following arguments:
106
-
-`queryset`: a queryset (or iterable) returned by the api function
107
-
-`pagination`- the paginator.Input parameters (parsed and validated)
108
-
-`**params`: kwargs that will contain all the arguments that decorated function received
109
-
98
+
- The `Input` schema is a Schema class that describes parameters that should be passed to your paginator (f.e. page-number or limit/offset values).
99
+
- The `Output` schema describes schema for page output (f.e. count/next-page/items/etc).
100
+
- The `paginate_queryset` method is passed the initial queryset and should return an iterable object that contains only the data in the requested page. This method accepts the following arguments:
101
+
-`queryset`: a queryset (or iterable) returned by the api function
102
+
-`pagination` - the paginator.Input parameters (parsed and validated)
103
+
-`**params`: kwargs that will contain all the arguments that decorated function received
110
104
111
105
Example:
112
106
@@ -119,7 +113,7 @@ class CustomPagination(PaginationBase):
119
113
# only `skip` param, defaults to 5 per page
120
114
classInput(Schema):
121
115
skip: int
122
-
116
+
123
117
124
118
classOutput(Schema):
125
119
items: List[Any] # `items` is a default attribute
@@ -163,12 +157,11 @@ class CustomPagination(PaginationBase):
163
157
results: List[Any]
164
158
total: int
165
159
per_page: int
166
-
160
+
167
161
items_attribute: str="results"
168
162
169
163
```
170
164
171
-
172
165
## Apply pagination to multiple operations at once
173
166
174
167
There is often a case when you need to add pagination to all views that returns querysets or list
@@ -195,7 +188,6 @@ In this example both operations will have pagination enabled
195
188
196
189
to apply pagination to main `api` instance use `default_router` argument:
0 commit comments