AltSchool / dynamic-rest

Dynamic extensions for Django REST Framework
MIT License
833 stars 111 forks source link

Conflict with filters #235

Open gonvaled opened 6 years ago

gonvaled commented 6 years ago

I am having problems with how drf, dynamic-rest and django_filters are playing together.

I have:

Python 3.6.4
Django==2.0.4
djangorestframework==3.7.7
dynamic-rest==1.8.1
django-filter==1.1.0

I have this defined:

    INSTALLED_APPS = (
        ...
        'rest_framework',            # utilities for rest apis
        'dynamic_rest',
        'django_filters',            # for filtering rest endpoints
        ...
    }

And this:

    REST_FRAMEWORK = {
        'DEFAULT_FILTER_BACKENDS': (
            'django_filters.rest_framework.DjangoFilterBackend',
        ),
    }

This does not work:

class PscViewSet(DynamicModelViewSet):
    queryset = PSC.objects.all()
    serializer_class = PscSerializer
    permission_classes = (AllowAny,)
    filter_fields = ('owner',)

No filtering is possible, and the Filters button is not shown in the browsable API.

Doing this:

class PscViewSet(DynamicModelViewSet):
    queryset = PSC.objects.all()
    serializer_class = PscSerializer
    permission_classes = (AllowAny,)
    filter_backends = (filters.DjangoFilterBackend,)   # This added
    filter_fields = ('owner',)

allows for filtering, but no Filters button is shown in the browsable API.

Doing this:

class PscViewSet(viewsets.ModelViewSet):
    queryset = PSC.objects.all()
    serializer_class = PscSerializer
    permission_classes = (AllowAny,)
    filter_fields = ('owner',)

allows for filtering and shows a Filters button in the browsable API.

denny64 commented 5 years ago

@gonvaled Did you find a solution to this?

TwunTee commented 5 years ago

Add DynamicFilterBackend to the list of filter backends

wolph commented 4 years ago

It took me a while to track down, but it's caused by the pagination_class that's automatically set by the DynamicModelViewSet. If you override that, it shows the filters again. For some reason the DynamicModelViewSet doesn't listen to any setting either, it always defaults your view's pagination_class to it's own.

But... after looking at the code I suspect the workings of dynamic-rest are really out of date from how django-rest-framework is handling sorting/pagination/etc. I noticed several differences between the overridden code and the DRF internals so for now I've stopped trying to integrate this library within my project. I find it a bit too invasive with the (seemingly) needless overriding of paginator classes, filter backends and more.

I believe it's in need of a pretty big update before the API browser is fully functional (again?).