• rest_framework使用完之后的简单总结


    首先先大致概括一下使用流程,因为还不是对这个框架很熟悉(其实有很多知识可以对比formModel的)

    其实还是遵循django的MTV的模式,还是得从url开始

    1、rest_framework有一个路由的写法,向下面这样

    router = DefaultRouter()

    router.register(r'procurements',view.ProcurementViewSet)

    urlpatterns = [

      url(r'^',include(router.urls)),

      url(r'^api-auth/',include('rest_framework.urls',namespace='rest_framework')),

      url(r'my_P/$',views.My_P_M_ProductList.as_views()),

      url(r'list/$',views.product_list_frame,name='product_list_frame'),

    ]^

    2、视图通常使用的是viewset类,然后在类名下面定义序列化,以及权限,或者过滤器,其中要使整个框架跑起来,一定要使用序列化,这个是必须的

    然后使用其他的功能的话,就在序列化的下面定义相应的功能,有些函数是默认的,只需要重写,在需要使用的位置会被自动调用

    类似于这段函数

    class ProcurementViewSet(viewsets.ModelViewSet):
    queryset = Procurement.objects.all()
    serializer_class = ProcurementSerializer
    def get_queryset(self):
    sum_procurement = Procurement.objects.all()
    quoted_procurements_list = Procurement.objects.annotate(quote_count=Count('quote')).filter(quote_count__gt=0)
    has_closed = Procurement.objects.filter(ptype=2)
    has_not_quote = list(set(sum_procurement).difference(quoted_procurements_list))
    has_not_quote = list(set(has_not_quote).difference(has_closed))
    # return sum_procurement
    return {'sum_procurement':sum_procurement,'quoted_procurements_list':quoted_procurements_list,'has_closed':has_closed,'has_not_quote':has_not_quote}
    def list(self, request, *args, **kwargs):
    baseQueryset = self.get_queryset()["sum_procurement"]
    if request.query_params.has_key('request_page') and request.query_params.has_key('max_line'):
    request_page = int(request.query_params['request_page'])
    max_line = int(request.query_params['max_line'])
    max_page = baseQueryset.count() / max_line
    if baseQueryset.count() % max_line > 0:
    max_page += 1
    if request_page <= max_page:
    start = max_line * (request_page - 1)
    end = start + max_line
    baseQueryset = baseQueryset[start:end]
    quotedproset = self.get_queryset()["quoted_procurements_list"]
    closeproset = self.get_queryset()["has_closed"]
    notquotedproset = self.get_queryset()["has_not_quote"]
    serializer = ProcurementSerializer(baseQueryset, many=True)
    serializerquoted = ProcurementSerializer(quotedproset,many=True)
    serializerclosed = ProcurementSerializer(closeproset,many=True)
    serializernotquoted = ProcurementSerializer(notquotedproset,many=True)
    if request.query_params.has_key('request_page') and request.query_params.has_key('max_line'):
    return Response({'serializerdata': serializer.data, 'max_page': max_page})
    elif request.query_params.has_key('quoted'):
    return Response({'serializerquoted':serializerquoted})
    elif request.query_params.has_key("closed"):
    return Response({'serializerclosed':serializerclosed})
    elif request.query_params.has_key('notquoted'):
    return Response({'serializernotquoted':serializernotquoted})
    3、序列化的定义类似于下面的代码
    class ProcurementSerializer(serializers.ModelSerializer):
    class Meta:
    model = Procurement
    权限的定义类似于下面的代码
    class IsOwnerOrReadOnly( permissions.BasePermission):

    def has_object_permission(self, request, view, obj):
    if request.method in permissions.SAFE_METHODS: #GET/HEAD/OPTIONS
    return True
    return obj.from_whom.member == request.user

    总结起来的意思就是url,视图(好几种不同的写法),还有序列化取数据,是功能基本使用的最基本的用法,其他的只要相应的在上面加就可以了
    部分功能参考链接:http://www.hi-roy.com//2016/06/27/django-REST-framework%E4%B8%AD%E6%96%87%E6%96%87%E6%A1%A3%E6%95%99%E7%A8%8B/
  • 相关阅读:
    Redis命令
    Linux命令
    SQL语句
    Redis集群
    Redis主主复制、主从复制
    关于Java乱码
    组合, 封装, 访问限制机制, property装饰器, 多态
    继承
    小练习
    面向过程与面向对象, 类和对象
  • 原文地址:https://www.cnblogs.com/yingqml/p/6650347.html
Copyright © 2020-2023  润新知