I tested the SearchFilter when I had around 10 records , and it was working fine, but I went ahead and tested it when the data records are above 200, it just returning the same data without searching or filtering the data , below is my View file :
class PostList(generics.ListCreateAPIView):
"""Blog post lists"""
queryset = Post.objects.filter(status=APPROVED)
serializer_class = serializers.PostSerializer
authentication_classes = (JWTAuthentication,)
permission_classes = (PostsProtectOrReadOnly, IsMentorOnly)
filter_backends = [DjangoFilterBackend, filters.SearchFilter]
filter_fields = ('title', 'body', 'description',)
search_fields = (
'@title',
'@body',
'@description',
)
def filter_queryset(self, queryset):
ordering = self.request.GET.get("order_by", None)
author = self.request.GET.get("author", None)
if ordering == 'blog_views':
queryset = queryset.annotate(
address_views_count=Count('address_views')).order_by(
'-address_views_count')
if author:
queryset = queryset.filter(owner__email=author)
return queryset
This is how I search :
/api/v1/blogs/?search=an elephant
But it just returns back all the data instead of filtering.
CodePudding user response:
Because you made an override of the filter_queryset
, it will no longer work with the filter_backends
to filter the data. You should filter the queryset further, by making a super call:
class PostList(generics.ListCreateAPIView):
# …
def filter_queryset(self, queryset):
ordering = self.request.GET.get('order_by', None)
author = self.request.GET.get('author', None)
# filter queryset with filter_backends