I am trying to filter data using a decorator action on Django Rest Framework, it works perfect if I use the global queryset (get_queryset() function) but I need to use it in a separate function.
I am using django-filter to perform it. This is the code.
My view:
class ShippingAPI(viewsets.ModelViewSet):
serializer_class = ShippingSerializer
filter_backends = (DjangoFilterBackend,)
filter_fields = ('origin__department', 'destination__department', 'first_collection_date', 'last_collection_date', 'vehicle')
The override (action)
@action(detail=False, methods=['GET'])
def filter_shippings(self, request, **kwargs):
queryset = Shipping.objects.filter(status=2, orderStatus=0)
serializer = SearchShippingSerializer(queryset, many=True) #Yes, I am using another serializer, but it is solved,I use diferent if it is necesary
return Response(serializer.data)
After use my url 'api/filter_shipping/(all filters here)', this still return all the data without the filters I am requesting.
Thanks for your help
Add filter_queryset function like this. It worked for me. Found the solution in Django-filters issues: https://github.com/carltongibson/django-filter/issues/967 .
@action(detail=False, methods=['GET'])
def filter_shippings(self, request, **kwargs):
queryset = self.filter_queryset(self.get_queryset()).filter(status=2, orderStatus=0)
serializer = SearchShippingSerializer(queryset, many=True) #Yes, I am using another serializer, but it is solved,I use diferent if it is necesary
return Response(serializer.data)
You can filter on the result of get_queryset
to limit your results.
@action(detail=False, methods=['GET'])
def filter_shippings(self, request, **kwargs):
queryset = self.get_queryset().filter(status=2, orderStatus=0)
serializer = SearchShippingSerializer(queryset, many=True) #Yes, I am using another serializer, but it is solved,I use diferent if it is necesary
return Response(serializer.data)
Edit: You can create a custom filter do the filtering as needed. Here is the example from django-filter's docs.
import django_filters
class ProductFilter(django_filters.FilterSet):
class Meta:
model = Product
fields = ['name', 'price', 'manufacturer']
def product_list(request):
filter = ProductFilter(request.GET, queryset=Product.objects.all())
return render(request, 'my_app/template.html', {'filter': filter})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With