Customizing pagination classes

The rest_framework.pagination.LimitOffsetPagination class that we are using to provide paginated responses declares a max_limit class attribute that defaults to None. This attribute allows us to indicate the maximum allowable limit that can be specified using the limit query parameter. With the default setting, there is no limit and we will be able to process requests that specify a value for 1000000 for the limit query parameter. We definitely don't want our API to be able to generate a response with a million player scores or players with a single request. Unluckily, there is no setting that allows us to change the value that the class assigns to the max_limit class attribute. Thus, we will create our customized version of the limit/offset pagination style provided by Django REST Framework.

Create a new Python file named pagination.py within the games folder and enter the following code which declares the new LimitOffsetPaginationWithMaxLimit class. The code file for the sample is included in the restful_python_chapter_03_03 folder:

from rest_framework.pagination import LimitOffsetPagination 
 
 
class LimitOffsetPaginationWithMaxLimit(LimitOffsetPagination): 
    max_limit = 10 

The preceding lines declare the LimitOffsetPaginationWithMaxLimit class as a subclass of the rest_framework.pagination.LimitOffsetPagination class and overrides the value specified for the max_limit class attribute with 10.

Open the gamesapi/settings.py file and replace the line that specified the value for the DEFAULT_PAGINATION_CLASS key in the dictionary named REST_FRAMEWORK with the highlighted line. The following lines show the new declaration of the dictionary named REST_FRAMEWORK. The code file for the sample is included in the restful_python_chapter_03_03 folder:

REST_FRAMEWORK = { 
    'DEFAULT_PAGINATION_CLASS': 
    'games.pagination.LimitOffsetPaginationWithMaxLimit', 
    'PAGE_SIZE': 5 
} 

Now, the generic views will use the recently declared games.pagination.LimitOffsetPaginationWithMaxLimit class, that provides a limit/offset based style with a maximum limit value equal to 10. If a request specifies a value for limit higher than 10, the class will use the maximum limit value, that is, 10, and we will never return more than 10 items in a paginated response.

Now, we will compose and send an HTTP request to retrieve the first page for the games, specifically an HTTP GET method to /games/ with the limit value set to 10000:

http GET ':8000/games/?limit=10000'

The following is the equivalent curl command:

curl -iX GET ':8000/games/?limit=10000'

The result will use a limit value equal to 10 instead of the indicated 10000 because we are using our customized pagination class. The result will provide us the first set with 10 game resources (results key), the total number of games for the query (count key), and a link to the next (next key) and previous (previous key) pages. In this case, the resultset is the first page, and therefore, the link to the next page (next key) is http://localhost:8000/games/?limit=10&offset=10. We will receive a 200 OK status code in the response header and the first 10 games in the results array. The following lines show the header and the first lines of the output:

HTTP/1.0 200 OK
Allow: GET, POST, HEAD, OPTIONS
Content-Type: application/json
Date: Fri, 01 Jul 2016 16:34:01 GMT
Server: WSGIServer/0.2 CPython/3.5.1
Vary: Accept, Cookie
X-Frame-Options: SAMEORIGIN
{
    "count": 12, 
    "next": "http://localhost:8000/games/?limit=10&offset=10", 
    "previous": null, 
    "results": [
        {

Tip

It is a good practice to configure a maximum limit to avoid generating huge responses.

Open a web browser and enter http://localhost:8000/games/. Replace localhost with the IP of the computer that is running the Django development server in case you use another computer or device to run the browser. The browsable API will compose and send a GET request to /games/ and will display the results of its execution, that is, the headers and the JSON games list; since we have configured pagination, the rendered web page will include the default pagination template associated with the base pagination class we are using and will display the available page numbers at the upper-right corner of the web page. The following screenshot shows the rendered web page after entering the URL in a web browser with the resource description, Game List, and the three pages.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.94.190