django-rest-framework-datatables icon indicating copy to clipboard operation
django-rest-framework-datatables copied to clipboard

Change behavior of json delivered by rest api

Open slamer59 opened this issue 6 years ago • 2 comments

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

slamer59 avatar Apr 03 '19 09:04 slamer59

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 avatar Apr 17 '19 15:04 izimobil

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__'

maysu1914 avatar Jul 02 '21 14:07 maysu1914