django-rest-framework 简写为-drf

来源:互联网 发布:淘宝onlyanna老公小苏 编辑:程序博客网 时间:2024/06/08 18:54
#自己序列化 
通过Django的View  model_to_dict方法  serializers JsonResponse来完成序列化

 


通过 django-rest-framework

class GoodsListViewSet(CacheResponseMixin, mixins.ListModelMixin, mixins.RetrieveModelMixin, viewsets.GenericViewSet):
    """
    商品列表页, 分页, 搜索, 过滤, 排序
    """
    # throttle_classes = (UserRateThrottle, )
    queryset = Goods.objects.all()
    serializer_class = GoodsSerializer
    pagination_class = GoodsPagination
    # authentication_classes = (TokenAuthentication, )
    filter_backends = (DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter)
    filter_class = GoodsFilter
    search_fields = ('name', 'goods_brief', 'goods_desc')

    ordering_fields = ('sold_num', 'shop_price')

很是方便   商品列表页, 分页, 搜索, 过滤, 排序  不需要太多的代码

Viewset组合使用 


#继承关系  绑定urls
GenericViewSet(viewset)       -drf
GenericAPIView            -drf
APIView               -drf
View              -django



Mixin
CreateModelMixin
ListModelMixin
RetriveModelMixin
UpdateModeMixin
DestroyModelMixin

原创粉丝点击