django filter on APIView django filter on APIView django django

django filter on APIView


To use the functionality of DjangoFilterBackend, you could incorporate the filter_queryset method from GenericViewSet, which is the DRF class that inherits from APIView and leads to all specific 'generic' view classes in DRF. It looks like this:

def filter_queryset(self, queryset):    """    Given a queryset, filter it with whichever filter backend is in use.    You are unlikely to want to override this method, although you may need    to call it either from a list view, or from a custom `get_object`    method if you want to apply the configured filtering backend to the    default queryset.    """    for backend in list(self.filter_backends):        queryset = backend().filter_queryset(self.request, queryset, self)    return queryset

https://github.com/encode/django-rest-framework/blob/master/rest_framework/generics.py


Here If you are using APIView, There is nothing to do with filters.So you have to do like

get_data = request.query_params #or request.GET check both

Then

Rental.objects.filter(city=get_data['city'], place=get_data['place'])


In case someone is wondering how can we integrate django_filters filter_class with api_views:

@api_view(['GET'])@permission_classes([permissions.IsAuthenticated])def filter_data(request, format=None):    qs = models.YourModal.objects.all()    filtered_data = filters.YourFilter(request.GET, queryset=qs)    filtered_qs = filtered_data.qs    ....    return response.Ok(yourData)