filter_class not working, what is going wrong?

1,062 views
Skip to first unread message

Gonzalo Amadio

unread,
May 28, 2018, 4:33:08 PM5/28/18
to django-filter
I have the following code.. but filtering is not working, it always returns all objects.

from django_filters import rest_framework as filters  
from rest_framework.response import Response
from rest_framework import mixins, viewsets
from back_emedido.apps.fault.serializer import FaultSerializer
from back_emedido.apps.fault.models import Fault              

     
class FaultFilter(filters.FilterSet):                                            
                                                                                                       
    class Meta:                                                                  
        model = Fault                                                            
        fields = ['patent']                                            
                                                                                 
class FaultViewSet(mixins.CreateModelMixin,                                      
           mixins.ListModelMixin,                                                
           mixins.RetrieveModelMixin,                                            
           mixins.UpdateModelMixin,                                              
           viewsets.GenericViewSet):                                             
                                                                       
    queryset = Fault.objects.all()                                               
    serializer_class = FaultSerializer                                           
    filter_class = FaultFilter                                                   
#    filter_backends = (filters.DjangoFilterBackend,)                            
#    filter_fields = ('patent',)                                                 
                                                                                 
    def list(self, request):                                                     
        ret = {'msg':'No faults on system.','data':{}, 'status':'ERR'}                                            
        serializer = FaultSerializer(self.get_queryset(), many=True)                                                                                                        
        ret = {"status":"OK", "msg":"ok", "data":serializer.data}
        return Response(ret)                                



Also with two commented lines instead of filter_class is not working

I am calling like this:   http http://10.11.11.60:8010/api/faults/?patent="OMJ865"

It only works if I override get_queryset, and get params from self.request.query_param.

My problem is that when I have 8 fields, I have to write all combinations.

For example, and so on:

if patent and phone:                                                
    queryset
= Parking.objects.filter(patent=patent, phone=phone)  
if phone:                                                          
    queryset
= Parking.objects.filter(phone=phone)            
if patent:                                                          
    queryset
= Parking.objects.filter(patent=patent)    

           


Gonzalo Amadio

unread,
May 28, 2018, 8:34:47 PM5/28/18
to django-filter
If I do not override "def list" , it works..

So the problem is calling self.get_queryset() . It does not take filters into account. It is always returning from this line :
 queryset = Fault.objects.all()

Carlton Gibson

unread,
May 29, 2018, 6:04:54 AM5/29/18
to django...@googlegroups.com
If you look at the default implementation you’ll see it calls `filter_queryset`.


def list(self, request, *args, **kwargs):
queryset = self.filter_queryset(self.get_queryset())

...

https://github.com/encode/django-rest-framework/blob/fe54575e6a0b1abc43a84814cc1b8625e6187a8b/rest_framework/mixins.py#L39-L40

You should do this too.

Regards,

Carlton

Gonzalo Amadio

unread,
May 30, 2018, 1:32:09 PM5/30/18
to django...@googlegroups.com
It works! .. thank you!


--
You received this message because you are subscribed to the Google Groups "django-filter" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-filter+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
--------
Gonzalo Amadio
Reply all
Reply to author
Forward
0 new messages