Working with customized pagination classes

We enabled pagination to limit the size for the result sets. However, any client or user is able to specify a large number for the limit value, such as 10000, and generate a huge result set. In order to specify the maximum number that is accepted for the limit query parameter, it is necessary to create a customized version of the limit/offset pagination scheme that the Django REST framework provides us.

We made changes to the global configuration to use the rest_framework.pagination.LimitOffsetPagination class to handle paginated responses. This class declares a max_limit class attribute whose default value is equal to None, which means there is no upper bound for the limit value. We will indicate the upper bound value for the limit query parameter in the max_limit class attribute.

Make sure you quit Django's development server. Remember that you just need to press Ctrl + C in the terminal or Command Prompt in which it is running.

Go to the restful01/drones folder and create a new file named custompagination.py. Write the following code in this new file. The following lines show the code for this file that declares the new LimitOffsetPaginationWithUpperBound class. The code file for the sample is included in the hillar_django_restful_07_02 folder in the restful01/drones/custompagination.py file:

from rest_framework.pagination import LimitOffsetPagination 
class LimitOffsetPaginationWithUpperBound(LimitOffsetPagination):
    # Set the maximum limit value to 8 
       max_limit = 8

The previous lines declare the LimitOffsetPaginationWithUpperBound class as a subclass of  rest_framework.pagination.LimitOffsetPagination. This new class overrides the value assigned to the max_limit class attribute with 8.

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

 REST_FRAMEWORK = { 
    'DEFAULT_PAGINATION_CLASS': 
    'drones.custompagination.LimitOffsetPaginationWithUpperBound', 
    'PAGE_SIZE': 4 
 } 

This way, all the generic views will use the recently declared drones.custompagination.LimitOffsetPaginationWithUpperBound class that provides the limit/offset pagination scheme we have analyzed with an upper bound for the limit value equal to 8.

If any request specifies a value higher than 8 for the limit, the class will use the maximum limit value, that is, 8, and the RESTful Web Service will never return more than eight resources in a paginated response.

It is a good practice to configure a maximum limit to avoid generating responses with huge amounts of data that might generate important loads to the server running the RESTful Web Service. Note that we will learn to limit the usage of the resources of our RESTful Web Service in the forthcoming chapters. Pagination is just the beginning of a long story.
..................Content has been hidden....................

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