gpt4 book ai didi

django-rest-framework - 覆盖列表时如何添加过滤器后端?

转载 作者:行者123 更新时间:2023-12-05 01:45:57 24 4
gpt4 key购买 nike

如果我覆盖列表,过滤器后端将不起作用。如果我覆盖列表定义,如何应用我的过滤器?

class CircuitViewSet(viewsets.ModelViewSet):
queryset = Circuit.objects.all()
serializer_class = CircuitSerializer
filter_backends = (filters.DjangoFilterBackend, filters.OrderingFilter,)
filter_class = CircuitFilter

def list(self, request):
queryset = Circuit.objects.all()
serializer = CircuitSerializer(queryset, many=True, context={'request': request})
return Response(serializer.data)

最佳答案

class CircuitViewSet(viewsets.ModelViewSet):
queryset = Circuit.objects.all()
serializer_class = CircuitSerializer
filter_backends = (filters.DjangoFilterBackend, filters.OrderingFilter,)
filter_class = CircuitFilter

def list(self, request):
# with filter
queryset = self.filter_queryset(self.get_queryset())

# pagination
page = self.paginate_queryset(queryset)
if page is not None:
serializer = self.get_serializer(page, many=True)
return self.get_paginated_response(serializer.data)

serializer = self.get_serializer(queryset, many=True)
return Response(serializer.data)

关于django-rest-framework - 覆盖列表时如何添加过滤器后端?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39138613/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com