wagtail / wagtail-generic-chooser

A toolkit for custom chooser popups in Wagtail
BSD 3-Clause "New" or "Revised" License
116 stars 25 forks source link

Access non-standard DRF response property for pagination. #47

Open dopry opened 2 years ago

dopry commented 2 years ago
generic_chooser\views.py", line 269, in get_paginated_object_list
    paginator = APIPaginator(result['meta']['total_count'], self.per_page)
KeyError: 'meta'

meta isn't a standard key in any of the base DRF responses. If this is meant to be the total count of objects in a paginated response it should use the count key, not [meta][total_count]

see: https://www.django-rest-framework.org/api-guide/pagination/

dopry commented 2 years ago

It looks like the get_paginated_object list for DRFChooserMixin should be updated to include the proper params for PageNumberPagination as well as LimitOffsetPagination. The records in the response are normally in the 'results' property not 'items'. Here is what worked for me.

def get_paginated_object_list(self, page_number, **kwargs):
        params = self.get_api_parameters(**kwargs)

        # support PageNumberPagination
        params['page'] = page_number
        params['page_size'] = self.per_page

       # support LimitOffsetPagination
        params['limit'] = self.per_page
        params['offset'] = (page_number - 1) * self.per_page

        result = requests.get(self.api_base_url, params=params).json()
        paginator = APIPaginator(result['count'], self.per_page)
        page = Page(result['results'], page_number, paginator)
        return (page, paginator)