Detailed explanation of the use of Django DRF pager



When there is too much data returned from the background, we need to configure the pager. For example, a page can only display 10 items at most,drfThree sub pages are configured by default in

  • Pagenumberpagination: basic pager with slightly poor performance
  • Limitoffsetpagination: offset pager
  • Cursorpagination: cursor pager with powerful performance


Basic pagerPageNumberPagination, the larger the amount of data, the worse the performance.

First of all, we areappCreate one inpagination.pyFile, and then customize a pager class, inherited fromPageNumberPagination

from rest_framework.pagination import PageNumberPagination
class MyPageNumberPagination(PageNumberPagination):
    Normal paging, the larger the amount of data, the worse the performance
    #Number of items displayed on the default page
    page_size = 3
    #The front-end access URL needs to be added? Page= page number
    page_query_param = 'page'
    #User defined number of returned entries, format? page_ Size= number of pages
    page_size_query_param = "page_size"
    #The maximum number of returned entries is user-defined. If the value exceeds 5, only 5 entries will be displayed
    max_page_size = 5

If we need local configuration, we can access and use it in the class viewpagination_class = MyPageNumberPaginationthat will do
If it is a global configuration, thesettings.pyThe configuration in the file is as follows:

    'DEFAULT_PAGINATION_CLASS': 'api.pagination.MyPageNumberPagination',

Then we visiturlJust add it later? Page= number of pagesthat will do
such ashttp://, representing the data accessing the second page, and the number of data is by defaultpage_sizeValue of
such ashttp://, representing the data accessed on the second page, and the number of returned entries customized by the user is 5


First, we customize a pager class that inherits fromLimitOffsetPagination

class MyLimitOffsetPagination(LimitOffsetPagination):
    #The URL is followed by the limit request parameter,
    limit_query_param = 'limit'
    #URL followed by offset request parameter
    offset_query_param = 'offset'
    #Maximum limit: 20
    max_limit = 20
    #3 default restrictions
    default_limit = 3

Then we visiturlJust add it later?limit=10that will do
such ashttp:// maximum number of data accessed by the representative is 10, if youlimitValue of >max_limit, then followmax_limitTo show the number of pieces of data
such ashttp://, not herelimitParameters, so 3 are displayed by default,offset=1When extracting data from the database list, it starts with subscript 1. For example, the extracted data list is['test1', 'test2', 'test3', 'test4']Originally, we extracted the first three by default, but you addedoffset=1After that, he starts to extract from the list with a subscript of 1, so the last extracted data istest2andtest3andtest4


The usage of the cursor pager is similar to that of the basic pager, except that the cursor pager is for the next page of dataurlEncrypted
First, we customize a pager class that inherits fromCursorPagination

class MyCursorPagination(CursorPagination):
    Cursor has high paging performance and safety
    page_size = 10
    page_size_query_param = "page_size"
    max_page_size = 20
    ordering = '-price'

If sorting filter is used in our viewfilter_backends = [OrderingFilter], then we are visitingurlParameters must be carried when? Ordering= fields to be sorted

The above is the details of the use of Django DRF pager. For more information about Django DRF pager, please pay attention to other related articles of developeppaer!

Recommended Today

Why is UTF-8 not recommended in MySQL

Recently, I encountered a bug. I tried to save a UTF-8 string in MariaDB encoded with “utf8” through rails, and then a strange error occurred: Incorrect string value: ‘\xF0\x9F\x98\x83 for column ‘summary’ at row 1 I use the UTF-8 encoded client, the server is also UTF-8 encoded, the database is also UTF-8 encoded, even the […]