DRF filter/search return everything?
Question:
I’m currently doing self project inventory app and trying to implement filter and search.
what I’ve done
#views.py
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework import filters
class ListInventories(generics.ListAPIView):
serializer_class = Inventory
filter_backends = [filters.SearchFilter]
search_fields = ['name']
filter_backends = [DjangoFilterBackend]
filterset_fields = ['name', 'stock']
def get(self, request):
inventories = Inventory.objects.all()
serializer = InventorySerializers(inventories, many=True)
return Response({'inventories': serializer.data})
class InventoryDetails(APIView):
def get(self, request, pk):
inventories = Inventory.objects.get(id=pk)
serializer = InventorySerializers(inventories)
return Response({'inventory': serializer.data})
in settings.py
INSTALLED_APPS = [
...
'django_filters',
...
]
REST_FRAMEWORK = {
'DEFAULT_FILTER_BACKENDS': ['django_filters.rest_framework.DjangoFilterBackend']
}
in url:
http://127.0.0.1:8000/api/inventory?name=para
I tried ?search=para
and ?search=anynonsense
. and the DRF would always return everything.
just for the sake of clarification here is the screenshot(the total of item is just 3 for now, but even then filter/search should work):
I expect the filter/search to function at least on a minimal level. What do I do to make this right?
Answers:
Cause of the error:
class ListInventories(generics.ListAPIView):
serializer_class = Inventory
# You are settings search filter here
* filter_backends = [filters.SearchFilter]
search_fields = ['name']
# and overriding it here, that's why search is not working
* filter_backends = [DjangoFilterBackend]
filterset_fields = ['name', 'stock']
it should be filter_backends = [DjangoFilterBackend, filters.SearchFilter]
After some time, I solved the problem.
It is the get
method that keep returning everything. I commented out the method get
and everything works fine.
I’m currently doing self project inventory app and trying to implement filter and search.
what I’ve done
#views.py
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework import filters
class ListInventories(generics.ListAPIView):
serializer_class = Inventory
filter_backends = [filters.SearchFilter]
search_fields = ['name']
filter_backends = [DjangoFilterBackend]
filterset_fields = ['name', 'stock']
def get(self, request):
inventories = Inventory.objects.all()
serializer = InventorySerializers(inventories, many=True)
return Response({'inventories': serializer.data})
class InventoryDetails(APIView):
def get(self, request, pk):
inventories = Inventory.objects.get(id=pk)
serializer = InventorySerializers(inventories)
return Response({'inventory': serializer.data})
in settings.py
INSTALLED_APPS = [
...
'django_filters',
...
]
REST_FRAMEWORK = {
'DEFAULT_FILTER_BACKENDS': ['django_filters.rest_framework.DjangoFilterBackend']
}
in url:
http://127.0.0.1:8000/api/inventory?name=para
I tried ?search=para
and ?search=anynonsense
. and the DRF would always return everything.
just for the sake of clarification here is the screenshot(the total of item is just 3 for now, but even then filter/search should work):
I expect the filter/search to function at least on a minimal level. What do I do to make this right?
Cause of the error:
class ListInventories(generics.ListAPIView):
serializer_class = Inventory
# You are settings search filter here
* filter_backends = [filters.SearchFilter]
search_fields = ['name']
# and overriding it here, that's why search is not working
* filter_backends = [DjangoFilterBackend]
filterset_fields = ['name', 'stock']
it should be filter_backends = [DjangoFilterBackend, filters.SearchFilter]
After some time, I solved the problem.
It is the get
method that keep returning everything. I commented out the method get
and everything works fine.