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 by 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 configuration setting that allows us to change the value that the class assigns to the max_limit
class attribute. Thus, we are forced to create our customized version of the limit
/offset
pagination style provided by Django REST Framework.
Â
Â
Create a new Python file named max_limit_pagination.py
within the games_service/games
folder and enter the following code that declares the...