Reputation: 1238
When I set a ViewSet with both filtering and list()
function -> filtering stops work:
class ClientViewSet(ModelViewSet):
serializer_class = ClientSerializer
queryset = Client.objects.all()
filter_class = ClientFilter
def list(self, request):
serializer = ClientSerializer(self.queryset, many=True)
return JsonResponse(serializer.data, safe=False)
Here is my filter class:
class ClientFilter(FilterSet):
type = CharFilter(name='type', lookup_expr='iexact')
parent_id = NumberFilter(name='parent__id')
title = CharFilter(name='title', lookup_expr='icontains')
class Meta:
model = Client
fields = {
'type', 'parent_id', 'title'
}
Please note
that without a list()
method filtering works perfectly, I checked that thousand times. I'm 100 % sure that list()
is exactly what causing the issue, I just don't know why and what exactly to do to solve this conflict.
Upvotes: 0
Views: 118
Reputation: 47364
You should use filter_queryset
method:
def list(self, request):
queryset = self.filter_queryset(self.queryset)
serializer = ClientSerializer(queryset, many=True)
return JsonResponse(serializer.data, safe=False)
Upvotes: 4