Skip to content

Commit d69339e

Browse files
docs: pagination docs update
1 parent 69fadf2 commit d69339e

File tree

1 file changed

+22
-30
lines changed

1 file changed

+22
-30
lines changed

docs/docs/guides/response/pagination.md

+22-30
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
**Django Ninja** comes with a pagination support. This allows you to split large result sets into individual pages.
44

5-
65
To apply pagination to a function - just apply `paginate` decorator:
76

87
```python hl_lines="1 4"
@@ -14,7 +13,6 @@ def list_users(request):
1413
return User.objects.all()
1514
```
1615

17-
1816
That's it!
1917

2018
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
2523

2624
by default limit is set to `100` (you can change it in your settings.py using `NINJA_PAGINATION_PER_PAGE`)
2725

28-
2926
## Built in Pagination Classes
3027

3128
### LimitOffsetPagination (default)
@@ -42,17 +39,18 @@ def list_users(request):
4239
```
4340

4441
Example query:
42+
4543
```
4644
/api/users?limit=10&offset=0
4745
```
4846

4947
this class has two input parameters:
5048

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)
5451

5552
### PageNumberPagination
53+
5654
```python hl_lines="1 4"
5755
from ninja.pagination import paginate, PageNumberPagination
5856

@@ -63,50 +61,46 @@ def list_users(request):
6361
```
6462

6563
Example query:
64+
6665
```
6766
/api/users?page=2
6867
```
6968

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 `page_size`, outputs 100 queryset per page by default (can be changed with settings.py)
7170

7271
Page numbering start with 1
7372

74-
you can also set custom page_size value individually per view:
73+
if you need limit certain endpoint higher or lower your default config you can inherit
74+
and customize `Input` class like this
7575

7676
```python hl_lines="2"
77-
@api.get("/users")
78-
@paginate(PageNumberPagination, page_size=50)
79-
def list_users(...
77+
class MyLargerPageNumberPagination(PageNumberPagination):
78+
class Input(PageNumberPagination.Input):
79+
page_size: int = Field(20, ge=1, le=1000)
8080
```
8181

82-
83-
8482
## Accessing paginator parameters in view function
8583

8684
If you need access to `Input` parameters used for pagination in your view function - use `pass_parameter` argument
8785

8886
In that case input data will be available in `**kwargs`:
8987

9088
```python hl_lines="2 4"
91-
@api.get("/someview")
92-
@paginate(pass_parameter="pagination_info")
93-
def someview(request, **kwargs):
94-
page = kwargs["pagination_info"].page
95-
return ...
89+
class MyLargerPageNumberPagination(PageNumberPagination):
90+
class Input(PageNumberPagination.Input):
91+
page_size: int = Field(1000, ge=1, le=1000)
9692
```
9793

98-
9994
## Creating Custom Pagination Class
10095

10196
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:
10297

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
110104

111105
Example:
112106

@@ -119,7 +113,7 @@ class CustomPagination(PaginationBase):
119113
# only `skip` param, defaults to 5 per page
120114
class Input(Schema):
121115
skip: int
122-
116+
123117

124118
class Output(Schema):
125119
items: List[Any] # `items` is a default attribute
@@ -163,12 +157,11 @@ class CustomPagination(PaginationBase):
163157
results: List[Any]
164158
total: int
165159
per_page: int
166-
160+
167161
items_attribute: str = "results"
168162

169163
```
170164

171-
172165
## Apply pagination to multiple operations at once
173166

174167
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
195188

196189
to apply pagination to main `api` instance use `default_router` argument:
197190

198-
199191
```python
200192
api = NinjaAPI(default_router=RouterPaginated())
201193

0 commit comments

Comments
 (0)