izimobil / django-rest-framework-datatables

Seamless integration between Django REST framework and Datatables.
http://django-rest-framework-datatables.readthedocs.io/en/latest/
MIT License
397 stars 89 forks source link

Change behavior of json delivered by rest api #39

Open slamer59 opened 5 years ago

slamer59 commented 5 years ago

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 commented 5 years ago

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.

maysu1914 commented 3 years ago

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