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
Copy file name to clipboardExpand all lines: docs/docs/guides/response/pagination.md
+76-19Lines changed: 76 additions & 19 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,6 @@
2
2
3
3
**Django Ninja** comes with a pagination support. This allows you to split large result sets into individual pages.
4
4
5
-
6
5
To apply pagination to a function - just apply `paginate` decorator:
7
6
8
7
```python hl_lines="1 4"
@@ -14,7 +13,6 @@ def list_users(request):
14
13
return User.objects.all()
15
14
```
16
15
17
-
18
16
That's it!
19
17
20
18
Now you can query users with `limit` and `offset` GET parameters
@@ -25,7 +23,6 @@ Now you can query users with `limit` and `offset` GET parameters
25
23
26
24
by default limit is set to `100` (you can change it in your settings.py using `NINJA_PAGINATION_PER_PAGE`)
27
25
28
-
29
26
## Built in Pagination Classes
30
27
31
28
### LimitOffsetPagination (default)
@@ -42,17 +39,18 @@ def list_users(request):
42
39
```
43
40
44
41
Example query:
42
+
45
43
```
46
44
/api/users?limit=10&offset=0
47
45
```
48
46
49
47
this class has two input parameters:
50
48
51
-
-`limit` - defines a number of queryset on the page (default = 100, change in NINJA_PAGINATION_PER_PAGE)
52
-
-`offset` - set's the page window offset (default: 0, indexing starts with 0)
53
-
49
+
-`limit` - defines a number of queryset on the page (default = 100, change in NINJA_PAGINATION_PER_PAGE)
50
+
-`offset` - set's the page window offset (default: 0, indexing starts with 0)
54
51
55
52
### PageNumberPagination
53
+
56
54
```python hl_lines="1 4"
57
55
from ninja.pagination import paginate, PageNumberPagination
58
56
@@ -63,11 +61,12 @@ def list_users(request):
63
61
```
64
62
65
63
Example query:
64
+
66
65
```
67
66
/api/users?page=2
68
67
```
69
68
70
-
this class has one parameter `page` and outputs 100 queryset per page by default (can be changed with settings.py)
69
+
this class has one parameter `page` and outputs 100 queryset per page by default (can be changed with settings.py)
71
70
72
71
Page numbering start with 1
73
72
@@ -82,12 +81,74 @@ def list_users(...
82
81
In addition to the `page` parameter, you can also use the `page_size` parameter to dynamically adjust the number of records displayed per page:
83
82
84
83
Example query:
84
+
85
85
```
86
86
/api/users?page=2&page_size=20
87
87
```
88
88
89
89
This allows you to temporarily override the page size setting in your request. The request will use the specified `page_size` value if provided. Otherwise, it will use either the value specified in the decorator or the value from `PAGINATION_MAX_PER_PAGE_SIZE` in settings.py if no decorator value is set.
90
90
91
+
### CursorPagination
92
+
93
+
Cursor-based pagination provides stable pagination for datasets that may change frequently. Cursor pagination uses base64 encoded tokens to mark positions in the dataset, ensuring consistent results even when items are added or removed.
94
+
95
+
```python hl_lines="1 4"
96
+
from ninja.pagination import paginate, CursorPagination
This class has a few parameters, which determine how the cursor position is ascertained and the parameter encoded:
122
+
123
+
-`ordering` - tuple of field names to order the queryset. Use `-` prefix for descending order. The first one of which will be used to encode the position. The ordering field should be unique if possible. A string representation of this field will be used to point to the current position of the cursor. Timestamps work well if each item in the collection is created independently. The paginator can handle some non-uniqueness by adding an offset. Defaults to `("-created",)`, change in `NINJA_PAGINATION_DEFAULT_ORDERING`
124
+
125
+
-`page_size` - default page size for endpoint. Defaults to `100`, change in `NINJA_PAGINATION_PER_PAGE`
126
+
-`max_page_size` - maximum allowed page size for endpoint. Defaults to `100`, change in `NINJA_PAGINATION_MAX_PER_PAGE_SIZE`
127
+
128
+
Finally, there is a `NINJA_PAGINATION_MAX_OFFSET` setting to limit malicious cursor requests. It defaults to `100`.
129
+
130
+
The class parameters can be set globally via settings as well as per view:
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:
109
169
110
-
- 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).
111
-
- The `Output` schema describes schema for page output (f.e. count/next-page/items/etc).
112
-
- 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:
113
-
-`queryset`: a queryset (or iterable) returned by the api function
114
-
-`pagination`- the paginator.Input parameters (parsed and validated)
115
-
-`**params`: kwargs that will contain all the arguments that decorated function received
116
-
170
+
- 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).
171
+
- The `Output` schema describes schema for page output (f.e. count/next-page/items/etc).
172
+
- 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:
173
+
-`queryset`: a queryset (or iterable) returned by the api function
174
+
-`pagination` - the paginator.Input parameters (parsed and validated)
175
+
-`**params`: kwargs that will contain all the arguments that decorated function received
117
176
118
177
Example:
119
178
@@ -126,7 +185,7 @@ class CustomPagination(PaginationBase):
126
185
# only `skip` param, defaults to 5 per page
127
186
classInput(Schema):
128
187
skip: int
129
-
188
+
130
189
131
190
classOutput(Schema):
132
191
items: List[Any] # `items` is a default attribute
@@ -170,12 +229,11 @@ class CustomPagination(PaginationBase):
170
229
results: List[Any]
171
230
total: int
172
231
per_page: int
173
-
232
+
174
233
items_attribute: str="results"
175
234
176
235
```
177
236
178
-
179
237
## Apply pagination to multiple operations at once
180
238
181
239
There is often a case when you need to add pagination to all views that returns querysets or list
@@ -202,7 +260,6 @@ In this example both operations will have pagination enabled
202
260
203
261
to apply pagination to main `api` instance use `default_router` argument:
0 commit comments