Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django filter on APIView

I have a APIView class for showing all the rents and posting and delete etc. Now i want search feature so i tried to use DjangoFilterBackend but it is not working. I see in documentation, it has been used with ListAPIView but how can i use it in APIView.

class Rent(APIView):
    """
    List all the rents if token is not provided else a token specific rent
    """
    serializer_class = RentSerializer
    filter_backends = (DjangoFilterBackend,)
    filter_fields = ('city', 'place', 'property_category',)
    search_fields = ('=city', '=place')
    def get(self, request, token=None, format=None):
        reply={}
        try:
            rents = Rental.objects.all()
            if token:
                rent = Rental.objects.get(token=token)
                reply['data'] = self.serializer_class(rent).data
            else:
                reply['data'] = self.serializer_class(rents, many=True).data
        except Rental.DoesNotExist:
            return error.RequestedResourceNotFound().as_response()
        except:
            return error.UnknownError().as_response()
        else:
            return Response(reply, status.HTTP_200_OK)

when i search the rent with the following parameters in the url, i get all the rents, instead i should get only those rents that lies in city Kathmandu and place koteshwor

http://localhost:8000/api/v1/rents?city=Kathmandu&place=Koteshwor

like image 267
Serenity Avatar asked May 11 '17 03:05

Serenity


People also ask

How do I filter Queryset in Django REST framework?

The simplest way to filter the queryset of any view that subclasses GenericAPIView is to override the . get_queryset() method. Overriding this method allows you to customize the queryset returned by the view in a number of different ways.

What is the purpose of filter () method in Django?

The filter() method is used to filter you search, and allows you to return only the rows that matches the search term.

What is Query_params in Django?

query_params is a more correctly named synonym for request. GET . For clarity inside your code, we recommend using request. query_params instead of the Django's standard request.


2 Answers

To use the functionality of DjangoFilterBackend, you could incorporate the filter_queryset method from GenericViewSet, which is the DRF class that inherits from APIView and leads to all specific 'generic' view classes in DRF. It looks like this:

def filter_queryset(self, queryset):
    """
    Given a queryset, filter it with whichever filter backend is in use.
    You are unlikely to want to override this method, although you may need
    to call it either from a list view, or from a custom `get_object`
    method if you want to apply the configured filtering backend to the
    default queryset.
    """
    for backend in list(self.filter_backends):
        queryset = backend().filter_queryset(self.request, queryset, self)
    return queryset

https://github.com/encode/django-rest-framework/blob/master/rest_framework/generics.py

like image 167
ChidG Avatar answered Sep 19 '22 22:09

ChidG


Here If you are using APIView, There is nothing to do with filters.So you have to do like

get_data = request.query_params #or request.GET check both

Then

Rental.objects.filter(city=get_data['city'], place=get_data['place'])
like image 23
itzMEonTV Avatar answered Sep 18 '22 22:09

itzMEonTV