class GoodsListViewset(CacheResponseMixin, mixins.ListModelMixin, mixins.RetrieveModelMixin, viewsets.GenericViewSet):
"""
利用Viewsets进一步优化,与urls中的Router配合
商品列表页
"""
throttle_classes = [UserRateThrottle, AnonRateThrottle] # 用户限速通过用户ID,匿名用户(未登录)限速通过IP地址
queryset = Goods.objects.all() # 属性
serializer_class = GoodsSerializer
pagination_class = GoodsPagination # 分页
def retrieve(self, request, *args, **kwargs):
instance = self.get_object()
instance.click_num += 1
instance += 1
serializer = self.get_serializer(instance)
return Response(serializer.data)
# authentication_classes = (TokenAuthentication, ) # 设置局部认证,列表页不需要登陆后访问,所以需注释掉
filter_backends = [DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter] # 过滤,搜索,排序
# filterset_fields = ['name', 'shop_price'] # drf中的过滤功能
#利用django-filter中的过滤功能,filterset_class实现复杂功能过滤
filterset_class = GoodsFilter
#利用drf 的filters 实现搜索和排序
search_fields = ['name', 'goods_brief', 'goods_desc']
ordering_fields = ['sold_num', 'shop_price']