5.6.通过Mixin+GenericAPIView实现API

通过Mixin+GenericAPIView实现API

class GoodsCategoryView(ListModelMixin, GenericAPIView):
    """
    获取商品类型列表
    """
    queryset = GoodsCategory.objects.all()[:10]
    serializer_class = GoodsCategorySerializer

    def get(self, request, *args, **kwargs):
        return self.list(request, *args, **kwargs)

通过ListAPIView实现API

通过ListAPIView可以省略mixins.ListModelMixin和GenericAPIView的继承,同时省略get、post、patch等函数

class GoodsCategoryView(ListAPIView):
    """
    获取商品类型列表
    """
    queryset = GoodsCategory.objects.all()[:10]
    serializer_class = GoodsCategorySerializer

分页

mo_shop/settings.py

自定义分页参数

Last updated

Was this helpful?