Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Rest Framework override viewset list() method without loosing filter_backends functionality

I have a viewset and I override list() method, but filtering by field stop working. How can I call filtering options from my code:

This is my viewset:

 class SupplementViewSet(viewsets.ModelViewSet):
    permission_classes = (permissions.IsAuthenticated,)
    queryset = models.Product.objects.filter()
    serializer_class = serializers.SuplementSerializer
    filter_backends = (DjangoFilterBackend, SearchFilter, OrderingFilter,)
    search_fields = ('hotel', 'name')
    filter_fields = ('id', 'hotel', 'name')

    def perform_create(self, instance):
        instance.save(product_type=models.Product.SUPPLEMENT)

    def list(self, request, pk=None):
        if pk == None:
            supplements = models.Product.objects.filter(product_type=models.Product.SUPPLEMENT)
        else:
            supplements =  models.Product.objects.get(product_type=models.Product.SUPPLEMENT, id=pk)

        page = self.paginate_queryset(supplements)
        if page is not None:
            serializer = self.get_serializer(page, many=True)
            return self.get_paginated_response(serializer.data)

        serializer = self.get_serializer(page, many=True)
        result_set = serializer.data

        return Response(result_set)

    def get_result_set(self, supplements):
        result_set = serializers.ProductSerializer(supplements, many=True).data

        return result_set

Thanks for the help.

like image 966
HuLu ViCa Avatar asked Jul 20 '18 01:07

HuLu ViCa


People also ask

What is ViewSet in Django REST framework?

A ViewSet class is simply a type of class-based View, that does not provide any method handlers such as . get() or . post() , and instead provides actions such as . list() and . create() .

What is difference between Api_view and ViewSet?

APIView allow us to define functions that match standard HTTP methods like GET, POST, PUT, PATCH, etc. Viewsets allow us to define functions that match to common API object actions like : LIST, CREATE, RETRIEVE, UPDATE, etc.

What is QuerySet in Django REST framework?

The root QuerySet provided by the Manager describes all objects in the database table. Usually, though, you'll need to select only a subset of the complete set of objects. The default behavior of REST framework's generic list views is to return the entire queryset for a model manager.


2 Answers

def list(self, request, pk=None):
    if pk == None:
        supplements = models.Product.objects.filter(product_type=models.Product.SUPPLEMENT)
    else:
        supplements =  models.Product.objects.filter(product_type=models.Product.SUPPLEMENT, id=pk)

    supplements= self.filter_queryset(supplements)
    page = self.paginate_queryset(supplements)

self.filter_queryset(queryset) is what you need, but it must accept queryset.

like image 52
Ykh Avatar answered Sep 29 '22 20:09

Ykh


It would be helpful to see the actual implementation of the list method:

http://www.cdrf.co/3.9/rest_framework.viewsets/ReadOnlyModelViewSet.html#list

You can directly use the self.filter_queryset method (which is inherited from GenericAPIView in viewsets.ModelViewSet). In very rare scenarios you will need to override the self.filter_queryset method.

like image 32
Akshay Anurag Avatar answered Sep 29 '22 18:09

Akshay Anurag