izimobil/django-rest-framework-datatables

Datatables incorrectly calculates the number of pages with custom filter_backend

TutajITeraz opened this issue · 0 comments

Hi,
First of all, thank you for creating this framework. It saved me a lot of time integrating datatables with django, especially since I have only been working in diango for 6 months!

The problem I encountered, which is not standardly supported by datatables, is that I needed to create a filter for two numeric values on one column ( less/greater than specified ) .

After reading the documentation:
https://django-rest-framework-datatables.readthedocs.io/en/latest/tutorial.html#filtering

I decided to create my own filter backend.

The problem I've encountered is that datatables now incorrectly calculates the number of pages, as if it didn't take into account that my filter was reducing the number of results

my ViewSet:

class ContentViewSet(viewsets.ModelViewSet):
    queryset = Content.objects.all().order_by('manuscript')
    serializer_class = ContentSerializer

    filter_backends = [CustomDatatablesFilterBackend]
    filterset_class = ContentGlobalFilter

And my CustomDatatablesFilterBackend:

class CustomDatatablesFilterBackend(DatatablesFilterBackend):

    def filter_queryset(self, request, queryset, view):
        queryset = super().filter_queryset(request, queryset, view)
        
        where_min = request.query_params.get('where_min')
        where_max = request.query_params.get('where_max')

        if where_min is not None:
            queryset = queryset.filter(where_in_ms_from__gte=where_min)
        
        if where_max is not None:
            queryset = queryset.filter(where_in_ms_from__lte=where_max)

        return queryset