当数据很多时 SearchFilter 行为不当

发布于 2025-01-11 09:46:04 字数 1167 浏览 0 评论 0原文

当我有大约 10 条记录时,我测试了 SearchFilter,它工作正常,但是当数据记录超过 200 条时,我继续测试它,它只返回相同的数据,而不搜索或过滤数据,下面是我的视图文件:

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

这就是我搜索的方式:

/api/v1/blogs/?search=an+elephant

但它只是返回所有数据而不是过滤。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

洋洋洒洒 2025-01-18 09:46:05

由于您覆盖了 filter_queryset,因此它将不再与 filter_backends 配合使用来过滤数据。您应该通过进行超级调用来进一步过滤查询集:

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

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 🖟
        queryset = super().filter_queryset(queryset)
        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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文