Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
dopry opened this issue Jan 26, 2022 · 1 comment
Open

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

dopry opened this issue Jan 26, 2022 · 1 comment

Comments

@dopry
Copy link

dopry commented Jan 26, 2022

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
Copy link
Author

dopry commented Jan 26, 2022

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant