arthas_dk
arthas_dk

Reputation: 443

Django 1.8 + DRF 3.4 + Django Filters 1.0.1 Won't Work on ViewSets Methods

I'm trying to use django-filters with django-rest-framework to implement get/url/params based filters, but it won't work with viewsets and shows no error. If I use a generics.ListAPIView for example, the filter works flawlessly! My project code:

Models.py

class OrderFeedBack(BaseModel):
    """
    Receive the customer rating, vendor and staff feedback about a order.

    Once the vendor or the customer has written his feedbacks, they can't change it.

    Developer: gcavalcante8808
    """
    rating = models.IntegerField()
    customer_feedback = models.CharField(max_length=255, null=True, blank=True)
    vendor_feedback = models.TextField(null=True, blank=True)
    staff_feedback = models.TextField(null=True, blank=True)
    order = models.ForeignKey("Order")
    locked = models.BooleanField(default=False)

Filters.py

class OrderFeedBackViewSet(viewsets.ViewSet):
    authentication_classes = (TokenAuthentication, SessionAuthentication)
    permission_classes = (IsAuthenticated,)
    filter_class = (OrderFeedBackFilter,)
    filter_backend = (filters.DjangoFilterBackend,)
    filter_fields = ('id', 'locked')
    search_fields = ('id', 'order',)


    def list(self, request):
        """
        List all Order Feedbacks.

        ---
        serializer: app.api_v1.serializers.OrderFeedBackSerializer
        omit_serializer: false

        responseMessages:
            - code: 401
              message: AuthenticationNeeded

            - code: 200
              message: List of Feedbacks.

        """
        data = OrderFeedBack.objects.all()
        serializer = OrderFeedBackSerializer(data, many=True)

        return Response(serializer.data)

Serializers.py

class OrderFeedBackSerializer(serializers.ModelSerializer):
    order = serializers.PrimaryKeyRelatedField(queryset=Order.objects.all())

    class Meta:
        model = OrderFeedBack

Even, if I drop the FieldSet class from "filter_class" or try to use "filter_class = OrderFeedBackFilter" it won't works as well.I have the following libraries installed in my virtualenv (and setUp in my settings.py):

Django==1.8.18
DjangoRestFramework==3.3
Django-Filters==1.0.1
Django-Crispy-Forms

I'm using the python 2.7.

Looking at the runserver logs, there is no error as well. Thanks in advance.

Upvotes: 2

Views: 293

Answers (2)

Sherpa
Sherpa

Reputation: 1998

The base APIView (and by extension, the base ViewSet) does not provide any integration with Django's models. This includes calling into the various filter backends (eg, DjangoFilterBackend), which are designed to work with models. You want to inherit from the GenericAPIViewSet instead.

Upvotes: 0

theguru42
theguru42

Reputation: 3378

django filters works automatically with generic views because if you will look at the source code it is using the filter class to filter out the query.

But in your list method you are not filtering out the query so it won't work. Use model view set [1] for the filter class to automatically work. Model view set inherits from generic api view.

Generic api view class has a method called filter_queryset which filters the query.

[1] http://www.django-rest-framework.org/api-guide/viewsets/#modelviewset

Upvotes: 1

Related Questions