LimitOffsetPagination
In LimitOffsetPagination style, client includes both a “limit” and an “offset” query parameter. The limit indicates the maximum number of items to return, same as that of the page_size. The offset indicates the starting position of the query w.r.t unpaginated items. To enable the LimitOffsetPagination style globally, you can set rest_framework.pagination.LimitOffsetPagination class to DEFAULT_PAGINATION_CLASS. The configuration as follows:
Python3
REST_FRAMEWORK = { 'DEFAULT_PAGINATION_CLASS' : 'rest_framework.pagination.LimitOffsetPagination' , 'PAGE_SIZE' : 2 , } |
You can skip setting the PAGE_SIZE. If set, then the client can omit the limit query parameter.
If you want to modify the pagination style, you can override the attributes of the LimitOffsetPagination class.
- default_limit – It indicates (numeric value) the limit. Defaults to the same value as the PAGE_SIZE settings key.
- limit_query_param – It indicates the name of the “limit” query parameter. Defaults to ‘limit’.
- offset_query_param – It indicates the name of the “offset” query parameter. Defaults to ‘offset’.
- max_limit – It indicates the maximum allowable limit that the client may request. Defaults to None.
- template – The template name to use when rendering pagination controls in the browsable API
The HTTPie command is
http :8000/robot/
Output
HTTP/1.1 200 OK Allow: GET, POST, HEAD, OPTIONS Content-Length: 541 Content-Type: application/json Date: Mon, 01 Feb 2021 06:47:42 GMT Referrer-Policy: same-origin Server: WSGIServer/0.2 CPython/3.7.5 Vary: Accept, Cookie X-Content-Type-Options: nosniff X-Frame-Options: DENY { "count": 4, "next": "http://localhost:8000/robot/?limit=2&offset=2", "previous": null, "results": [ { "currency": "USD", "currency_name": "US Dollar", "manufacturer": "Fanuc", "manufacturing_date": "2019-10-12T00:00:00Z", "name": "FANUC M-710ic/50", "price": 37000, "robot_category": "Articulated Robots", "url": "http://localhost:8000/robot/1/" }, { "currency": "USD", "currency_name": "US Dollar", "manufacturer": "ABB", "manufacturing_date": "2020-05-10T00:00:00Z", "name": "IRB 910SC", "price": 27000, "robot_category": "SCARA Robots", "url": "http://localhost:8000/robot/2/" } ] }
Let’s try another HTTPie command based on the next field value from the above output. The HTTPie command is
http GET “:8000/robot/?limit=2&offset=2”
Output
HTTP/1.1 200 OK Allow: GET, POST, HEAD, OPTIONS Content-Length: 524 Content-Type: application/json Date: Mon, 01 Feb 2021 06:52:35 GMT Referrer-Policy: same-origin Server: WSGIServer/0.2 CPython/3.7.5 Vary: Accept, Cookie X-Content-Type-Options: nosniff X-Frame-Options: DENY { "count": 4, "next": null, "previous": "http://localhost:8000/robot/?limit=2", "results": [ { "currency": "USD", "currency_name": "US Dollar", "manufacturer": "Fanuc", "manufacturing_date": "2020-02-12T00:00:00Z", "name": "M-10iD/8L", "price": 20000, "robot_category": "Articulated Robots", "url": "http://localhost:8000/robot/4/" }, { "currency": "USD", "currency_name": "US Dollar", "manufacturer": "Fanuc", "manufacturing_date": "2020-02-12T00:00:00Z", "name": "SR-6iA", "price": 10000, "robot_category": "SCARA Robots", "url": "http://localhost:8000/robot/5/" } ] }
Sharing the command prompt screenshot for your reference
Let’s try with limit=1 and offset=0. The HTTPie command is:
http GET “:8000/robot/?limit=1&offset=0”
Output
HTTP/1.1 200 OK Allow: GET, POST, HEAD, OPTIONS Content-Length: 325 Content-Type: application/json Date: Mon, 01 Feb 2021 10:36:19 GMT Referrer-Policy: same-origin Server: WSGIServer/0.2 CPython/3.7.5 Vary: Accept, Cookie X-Content-Type-Options: nosniff X-Frame-Options: DENY { "count": 4, "next": "http://localhost:8000/robot/?limit=1&offset=1", "previous": null, "results": [ { "currency": "USD", "currency_name": "US Dollar", "manufacturer": "Fanuc", "manufacturing_date": "2019-10-12T00:00:00Z", "name": "FANUC M-710ic/50", "price": 37000, "robot_category": "Articulated Robots", "url": "http://localhost:8000/robot/1/" } ] }
Sharing the command prompt screenshot
Adding Pagination in APIs – Django REST Framework
Imagine you have huge amount of details in your database. Do you think that it is wise to retrieve all at once while making an HTTP GET request? Here comes the importance of the Django REST framework pagination feature. It facilitates splitting the large result set into individual pages of data for each HTTP request.
So when we make an HTTP request, we must specify the details for the specific pages that we want to retrieve, and it will be based on predefined pagination schemes. Apart from retrieving data as pages, it also provides information about the total number of data, the next page, and the previous one in the response section.
- PageNumberPagination
- LimitOffsetPagination
- CursorPagination
Note: You can refer The Browsable API section for Models, Serializers, and Views of Project used in the article