• restframework CBV试图的4种方式


    day100



    上节回顾

    1 APIView流程

    2 序列化组件(restframework)

    1 # 自定义序列化组件
    class BookSerializers(serializers.Serializer):
    title=serializers.CharField()
    price=serializers.CharField()
    publishDate=serializers.DateField()

    # 针对一对多
    publish=serializers.CharField(source="publish.name")
    publish_email=serializers.CharField(source="publish.email")
    # 针对多对多
    authors=serializers.SerializerMethodField()
    def get_authors(self,obj):
    data=[]
    for i in obj.authors.all():
    temp=[]
    temp.append(i.pk)
    temp.append(i.name)
    data.append(temp)
    return data
    # ModelSerializer序列化组件
    class BookSerializers(serializers.ModelSerializer):
    class Meta:
    model=Book
    fields="__all__"

    publish = serializers.CharField(source="publish.name")
    authors=serializers.SerializerMethodField()
    def get_authors(self,obj):
    data=[]
    for i in obj.authors.all():
    temp=[]
    temp.append(i.pk)
    temp.append(i.name)
    data.append(temp)
    return data


    2 5个接口

    "books" GET:获取所有数据
    POST:添加一条数据

    "book/1" GET:获取单条数据
    DELETE:删除一条数据
    PUT/PATCH:更新一条数据


    3
    (1) 功能之序列化:
    book_list = Book.objects.all()
    ps = BookSerializers(book_list, many=True)
    ps.data

    book_obj=Book.objects.filter(pk=pk).first()
    bs=BookSerializers(book_obj,many=False)

    (2) 校验字段与操作记录(ModelSerializer拥有)
    # 添加数据
    bs=BookSerializers(data=request.data)
    if bs.is_valid(): #校验字段接口
    bs.save() # 生成记录接口
    return Response(bs.data) # 序列化接口


    # 更新数据
    bs=BookSerializers(data=request.data,instance=book_obj)
    if bs.is_valid(): #校验字段接口
    bs.save() #更新记录接口
    return Response(bs.data) # 序列化接口


    3 视图

    (1)

    class AuthorView(APIView):
    def get():
    pass
    def post():
    pass

    class AuthorDetailView(APIView):
    def get():
    pass
    def delete():
    pass
    def put():
    pass

    (2) class AuthorView(CreateModelMixin,ListModelMixin,generics.GenericAPIView,):

    queryset=Author.objects.all
    serializer_class=AuthorSerializers

    def get(self,request):
    return self.list(request)
    def post(self,request):

    return self.create(request)

    class AuthorDetailView(DestroyModelMixin,UpdateModelMixin,RetrieveModelMixin,generics.GenericAPIView,):
    queryset = Author.objects.all
    serializer_class = AuthorSerializers

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

    def put(self,request, *args, **kwargs):
    return self.update(request, *args, **kwargs)

    def delete(self,request, *args, **kwargs):
    return self.destroy(request, *args, **kwargs)
    (3) class AuthorView(generics.ListCreateAPIView):

    queryset=Author.objects.all
    serializer_class=AuthorSerializers

    class AuthorDetailView(generics.RetrieveUpdateDestroyAPIView):
    queryset = Author.objects.all
    serializer_class = AuthorSerializers


    (4) class AuthorModelView(ModelViewSet):

    queryset=Author.objects.all()
    serializer_class=AuthorSerializers

    ViewSetMixIn


        第4种的url要传参数,注意 如:

          url(r'^publishs/$',views.PublishView.as_view({"get":"list","post":"create"})),
      
    url(r'^publishs/(?P<pk>d+)/$',views.PublishView.as_view({"put":"update","get":"retrieve","delete":"destroy"}))


    4 解析器
    class APIView:
    parser_classes = api_settings.DEFAULT_PARSER_CLASSES


  • 相关阅读:
    ubuntu创建用户命令
    C#图像处理(各种旋转、改变大小、柔化、锐化、雾化、底片、浮雕、黑白、滤镜效果)
    Ubuntu14.04 64bit 编译安装nginx1.7+php5.4+mysql5.6
    ubuntu mysql 远程连接问题解决方法
    如何在LabWIndows/CVI中调用LabVIEW DLL
    NI MAX中缺少串口(转)
    LabWindows/CVI入门之第四章:库文件(转)
    c#中多线程同步Lock(锁)的研究以及跨线程UI的操作 (转)
    C# 实现生产者消费者队列 (转)
    Unity3d基于Socket通讯例子(转)
  • 原文地址:https://www.cnblogs.com/zhaoweihang/p/9407876.html
Copyright © 2020-2023  润新知