'SearchFilter misbehaves when data is a lot
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
