Skip to content

Datatables incorrectly calculates the number of pages with custom filter_backend #144

@TutajITeraz

Description

@TutajITeraz

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions