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

Change behavior of json delivered by rest api #39

Open
slamer59 opened this issue Apr 3, 2019 · 2 comments
Open

Change behavior of json delivered by rest api #39

slamer59 opened this issue Apr 3, 2019 · 2 comments
Labels
enhancement New feature or request

Comments

@slamer59
Copy link

slamer59 commented Apr 3, 2019

Hello,
I wonder if it's is normal to change the behavior of json request, eg. ?format=json

I have different API and I just want to use this library for one of them.

For exemple,

{"id":1,"label":"20181002-165328","bounds":[0,-1]},
{"id":2,"label":"20181002-165328","bounds":[0,-1]]

becomes

{"count":2,"next":null,"previous":null,"results":[{"id":1,"label":"20181002-165328","bounds":[0,-1],{"id":2,"label":"20181002-165328","bounds":[0,-1]}

Is it the expected behavior ?

Regards

@izimobil
Copy link
Owner

Hi,
This is the expected behavior, the DRF-Datatables pagination class delegates pagination to it's parent class when format is different from "datatables", hence the paginated results.

That said, you raise an interesting question, maybe it would be useful to add an option to change this behavior, something like datatables_dont_paginate_non_datatables_format̀ (quite a long name !).

I leave the issue opened as a reminder.

Regards.

@izimobil izimobil added the enhancement New feature or request label Apr 17, 2019
@maysu1914
Copy link
Contributor

maysu1914 commented Jul 2, 2021

I wrote a parent class to override the paginator method. Easy to implement. Maybe it will help someone.

class CustomListAPIView(generics.ListAPIView):
    @property
    def paginator(self):
        """
        The paginator instance associated with the view, or `None`.
        """
        if not hasattr(self, '_paginator'):
            if self.pagination_class is None:
                self._paginator = None
            elif self.request.GET.get('format', None) == 'datatables':
                self._paginator = self.pagination_class()
            else:
                self._paginator = None
        return self._paginator

class SomeList(CustomListAPIView):
    queryset = Some.objects.prefetch_related("others").all()
    serializer_class = SomeSerializer
    filter_backends = [OrderingFilter]
    ordering_fields = '__all__'

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

No branches or pull requests

3 participants