Kuracha
Kuracha

Reputation: 321

Creating custom Search filter in Django API view

I have my custom API view and I want to use Search filter in this view, but generic filter dodn't work so i want to create custom one but It doesn't work and I dont know where is problem.

view

class TaskIndexAPIView(APIView):
filter_backends = (CustomSearchFilter,)
search_fields = ('name', 'description', 'user__username')
def get_queryset(self):
    return Task.objects.all()

def get(self, request):
    tasks = self.get_queryset()
    for i in tasks:
        if i.date <= date.today():
            i.delayed = 'This task is delayed'
            i.save()
        else:
            i.delayed = ''
            i.save()

    serializer = IndexSerializer(tasks, many=True)
    return Response(serializer.data)

My custom search filter

search_filter

class CustomSearchFilter(filters.SearchFilter):
def get_search_fields(self, view, request):
    if request.get_queryset.get('name', 'user'):
        return ['name', 'user']
    return super(CustomSearchFilter, self).get_search_fields(view, request)

Upvotes: 2

Views: 7155

Answers (1)

JPG
JPG

Reputation: 88619

In your context, the difference between APIView and generic view is, the generic view has a method called filter_queryset() which handles the filtering and searching operations.

So here in your view, we need to include the same.

class TaskIndexAPIView(APIView):
    filter_backends = (CustomSearchFilter,)
    search_fields = ('name', 'description', 'user__username')

    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

    def get_queryset(self):
        return Task.objects.all()

    def get(self, request):
        the_filtered_qs = self.filter_queryset(self.get_queryset())

        tasks = the_filtered_qs
        for i in tasks:
            if i.date <= date.today():
                i.delayed = 'This task is delayed'
                i.save()
            else:
                i.delayed = ''
                i.save()

        serializer = IndexSerializer(tasks, many=True)
        return Response(serializer.data)

you will get the filtered queryset as

the_filtered_qs = self.filter_queryset(self.get_queryset())

Upvotes: 2

Related Questions