4
4
The DJA package implements a custom renderer, parser, exception handler, query filter backends, and
5
5
pagination. To get started enable the pieces in ` settings.py ` that you want to use.
6
6
7
- Many features of the [ JSON: API ] ( http://jsonapi.org/format ) format standard have been implemented using
7
+ Many features of the [ JSON: API ] ( http://jsonapi.org/format ) format standard have been implemented using
8
8
Mixin classes in ` serializers.py ` .
9
9
The easiest way to make use of those features is to import ModelSerializer variants
10
10
from ` rest_framework_json_api ` instead of the usual ` rest_framework `
@@ -16,7 +16,7 @@ REST_FRAMEWORK = {
16
16
' PAGE_SIZE' : 10 ,
17
17
' EXCEPTION_HANDLER' : ' rest_framework_json_api.exceptions.exception_handler' ,
18
18
' DEFAULT_PAGINATION_CLASS' :
19
- ' rest_framework_json_api.pagination.JsonApiPageNumberPagination ' ,
19
+ ' rest_framework_json_api.pagination.PageNumberPagination ' ,
20
20
' DEFAULT_PARSER_CLASSES' : (
21
21
' rest_framework_json_api.parsers.JSONParser' ,
22
22
' rest_framework.parsers.FormParser' ,
@@ -41,6 +41,8 @@ REST_FRAMEWORK = {
41
41
),
42
42
' TEST_REQUEST_DEFAULT_FORMAT' : ' vnd.api+json'
43
43
}
44
+
45
+ JSON_API_STANDARD_PAGINATION = True
44
46
```
45
47
46
48
### Pagination
@@ -50,6 +52,8 @@ DJA pagination is based on [DRF pagination](https://www.django-rest-framework.or
50
52
When pagination is enabled, the renderer will return a ` meta ` object with
51
53
record count and a ` links ` object with the next, previous, first, and last links.
52
54
55
+ Optional query parameters can also be provided to customize the page size or offset limit.
56
+
53
57
#### Configuring the Pagination Style
54
58
55
59
Pagination style can be set on a particular viewset with the ` pagination_class ` attribute or by default for all viewsets
@@ -58,36 +62,46 @@ by setting `REST_FRAMEWORK['DEFAULT_PAGINATION_CLASS']` and by setting `REST_FRA
58
62
You can configure fixed values for the page size or limit -- or allow the client to choose the size or limit
59
63
via query parameters.
60
64
65
+ You should also set ` JSON_API_STANDARD_PAGINATION=True ` in order to get up to date JSON: API-style default
66
+ query parameters and pagination size limits. The default value (False) sets deprecated values that will
67
+ be eliminated in the future. See below.
68
+
61
69
Two pagination classes are available:
62
- - ` JsonApiPageNumberPagination ` breaks a response up into pages that start at a given page number with a given size
70
+ - ` PageNumberPagination ` breaks a response up into pages that start at a given page number with a given size
63
71
(number of items per page). It can be configured with the following attributes:
64
- - ` page_query_param ` (default ` page[number] ` )
65
- - ` page_size_query_param ` (default ` page[size] ` ) Set this to ` None ` if you don't want to allow the client
66
- to specify the size.
67
- - ` max_page_size ` (default ` 100 ` ) enforces an upper bound on the ` page_size_query_param ` .
72
+ - ` page_size ` (default ` REST_FRAMEWORK['PAGE_SIZE'] ` ) number of items per page.
73
+ - ` max_page_size ` (default ` 100 ` ) enforces an upper bound on the page size.
68
74
Set it to ` None ` if you don't want to enforce an upper bound.
69
- - ` JsonApiLimitOffsetPagination ` breaks a response up into pages that start from an item's offset in the viewset for
75
+ - ` page_query_param ` (if ` JSON_API_STANDARD_PAGINATION is True ` : ` page[number] ` otherwise ` page ` ) is the query
76
+ parameter for the page number.
77
+ - ` page_size_query_param ` (if ` JSON_API_STANDARD_PAGINATION is True ` : ` page[size] ` otherwise ` page_size ` ) is the
78
+ query parameter for the page size. No more than ` max_page_size ` items will be returned.
79
+ Set this to ` None ` if you don't want to allow the client to specify the size.
80
+ - ` LimitOffsetPagination ` breaks a response up into pages that start from an item's offset in the viewset for
70
81
a given number of items (the limit).
71
82
It can be configured with the following attributes:
72
- - ` offset_query_param ` (default ` page[offset] ` ).
73
- - ` limit_query_param ` (default ` page[limit] ` ).
74
- - ` max_limit ` (default ` 100 ` ) enforces an upper bound on the limit.
75
- Set it to ` None ` if you don't want to enforce an upper bound.
76
-
83
+ - ` default_limit ` (default ` REST_FRAMEWORK['PAGE_SIZE'] ` ) is the number of items per page.
84
+ - ` offset_query_param ` (default ` page[offset] ` ) is the query parameter that sets the offset (item number) to return.
85
+ - ` limit_query_param ` (default ` page[limit] ` ) is the query parameter that sets the limit (number of items per page).
86
+ No more than ` max_limit ` items will be returned.
87
+ - ` max_limit ` (if ` JSON_API_STANDARD_PAGINATION is True ` : ` 100 ` otherwise ` None ` ) enforces an upper bound on the
88
+ limit. Set it to ` None ` if you don't want to enforce an upper bound.
77
89
78
90
These examples show how to configure the parameters to use non-standard names and different limits:
79
91
80
92
``` python
81
- from rest_framework_json_api.pagination import JsonApiPageNumberPagination, JsonApiLimitOffsetPagination
93
+ from rest_framework_json_api.pagination import PageNumberPagination, LimitOffsetPagination
82
94
83
- class MyPagePagination (JsonApiPageNumberPagination ):
95
+ class MyPagePagination (PageNumberPagination ):
84
96
page_query_param = ' page_number'
85
- page_size_query_param = ' page_size'
97
+ page_size_query_param = ' page_length'
98
+ page_size = 3
86
99
max_page_size = 1000
87
100
88
- class MyLimitPagination (JsonApiLimitOffsetPagination ):
101
+ class MyLimitPagination (LimitOffsetPagination ):
89
102
offset_query_param = ' offset'
90
103
limit_query_param = ' limit'
104
+ default_limit = 3
91
105
max_limit = None
92
106
```
93
107
@@ -146,7 +160,7 @@ If you are also using [`rest_framework.filters.SearchFilter`](https://django-res
146
160
(which performs single parameter searchs across multiple fields) you'll want to customize the name of the query
147
161
parameter for searching to make sure it doesn't conflict with a field name defined in the filterset.
148
162
The recommended value is: ` search_param="filter[search]" ` but just make sure it's
149
- ` filter[_something_] ` to comply with the jsonapi spec requirement to use the filter
163
+ ` filter[_something_] ` to comply with the JSON : API spec requirement to use the filter
150
164
keyword. The default is "search" unless overriden.
151
165
152
166
The filter returns a ` 400 Bad Request ` error for invalid filter query parameters as in this example
@@ -445,7 +459,7 @@ class OrderSerializer(serializers.ModelSerializer):
445
459
446
460
```
447
461
448
- In the [ JSON API spec] ( http://jsonapi.org/format/#document-resource-objects ) ,
462
+ In the [ JSON: API spec] ( http://jsonapi.org/format/#document-resource-objects ) ,
449
463
relationship objects contain links to related objects. To make this work
450
464
on a serializer we need to tell the ` ResourceRelatedField ` about the
451
465
corresponding view. Use the ` HyperlinkedModelSerializer ` and instantiate
@@ -583,7 +597,7 @@ class OrderSerializer(serializers.HyperlinkedModelSerializer):
583
597
### RelationshipView
584
598
` rest_framework_json_api.views.RelationshipView ` is used to build
585
599
relationship views (see the
586
- [ JSON API spec] ( http://jsonapi.org/format/#fetching-relationships ) ).
600
+ [ JSON: API spec] ( http://jsonapi.org/format/#fetching-relationships ) ).
587
601
The ` self ` link on a relationship object should point to the corresponding
588
602
relationship view.
589
603
0 commit comments