将def list()添加到模型视图集时,Django rest框架过滤器和搜索不起作用 [英] Django rest framework filters and search not working when def list() is added to model viewsets
本文介绍了将def list()添加到模型视图集时,Django rest框架过滤器和搜索不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在研究用Django rest框架编写的API。我在ModelViewSet中添加了搜索过滤器和排序过滤器,并且运行良好。
I am working on API written in Django rest framework. I have added Search filters and ordering filters in my ModelViewSet and were working fine.
class bookviewset(ModelViewSet):
queryset = Book.objects.all()
serializer_class = book_serializer
filter_class = bookfilter
filter_backends = ( django_filters.rest_framework.DjangoFilterBackend,filters.OrderingFilter,filters.SearchFilter)
ordering_fields = ('created_at', 'id','price_ids__price',)
search_fields = ('name', 'description', 'tag_ids__tag_name', 'category_ids__category')
但是,当我覆盖 def list(self,request,* args,** kwargs):
在Modelviewset中,所有过滤器均已停止工作。
But, When I override def list(self, request, *args, **kwargs):
inside Modelviewset, all the filters have stopped working.
有办法再次启用所有过滤器吗?
Is there a way to enable all the filters again?
谢谢。
推荐答案
您必须使用 filter_queryset
方法手动过滤查询集。因此,将此行添加到您的列表方法中。
You must manually filter your queryset using this filter_queryset
method. So add this line to your list method.
def list(self, request, *args, **kwargs):
# fetch qs
qs = self.filter_queryset(qs)
# etc
这篇关于将def list()添加到模型视图集时,Django rest框架过滤器和搜索不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文