zoukankan      html  css  js  c++  java
  • drf 多表

    https://www.django-rest-framework.org/  官方站

    https://www.django-rest-framework.org/tutorial/quickstart/#serializers    -- 序列化

    一个 例子

    from django.conf.urls import url
    from django.contrib import admin
    from app01 import views
    
    urlpatterns = [
        url(r'^admin/', admin.site.urls),
        url(r'books/(d+)/', views.BookHandle.as_view()),
        url(r'books/', views.BookHandle.as_view()),
    ]
    class BookHandle(APIView):
        # 获取所有书籍
        def get(self, request,id=None):
            if id:
                books_obj_list = models.Book.objects.filter(id=id)
            else:
                books_obj_list = models.Book.objects.all()
    
            book_se = BookSerializer(books_obj_list, many=True)  # 要点
            return Response(book_se.data)

    查询全部和带参数的查询 -- 

    from django.shortcuts import render
    from app01 import models
    from rest_framework.views import APIView
    from rest_framework import serializers
    from rest_framework.response import Response   # 响应
    
    class BookSerializer(serializers.ModelSerializer):
        class Meta:
            model = models.Book
            fields = '__all__'  # exclude = ('user',) 排除某些字段的方法    fields=['','']列表
         class BookHandle(APIView): # 获取所有书籍 def get(self, request,id=None): if id: books_obj_list = models.Book.objects.filter(nid=id) else: books_obj_list = models.Book.objects.all() book_se = BookSerializer(books_obj_list, many=True) # 要点 return Response(book_se.data) def post(self, request): cs = BookSerializer(data=request.data,many=False) if cs.is_valid(): cs.save() # create 新建 return Response(cs.data) else: return Response(cs.errors) class SBookHandle(APIView): # 更新数据 def put(self,request,id): old_obj = models.Book.objects.filter(pk=id).first() # querySet cs = BookSerializer(data=request.data,many=False,instance=old_obj) if cs.is_valid(): cs.save() # 更新 update return Response(cs.data) else: return Response(cs.errors) def delete(self,request,id): models.Book.objects.filter(pk=id).delete() # 删除 return Response('') def get(self,request,id): books_obj_list = models.Book.objects.get(pk=id) # model 类型 book_se = BookSerializer(books_obj_list,many=False) return Response(book_se.data)
    from django.conf.urls import url
    from django.contrib import admin
    from app01 import views
    
    urlpatterns = [
        url(r'^admin/', admin.site.urls),
    
        url(r'books/$', views.BookHandle.as_view()),  # 不带参数的
    
        url(r'books/(d+)/', views.SBookHandle.as_view()), # 带参数的
    ]
     -- views.py --
    # 进阶版
    from rest_framework.mixins import CreateModelMixin,UpdateModelMixin,DestroyModelMixin,ListModelMixin,RetrieveModelMixin
    from rest_framework import generics
    
    class PublishHandle(ListModelMixin,CreateModelMixin,generics.GenericAPIView):
        queryset = models.Publish.objects.all()
        serializer_class = PublishSerializer
        def get(self, request):
    
            return self.list(request)
    
        def post(self, request):
            return self.create(request)
    
    class SPublishHandle(UpdateModelMixin,DestroyModelMixin,RetrieveModelMixin, generics.GenericAPIView):
        queryset = models.Publish.objects.all()
        serializer_class = PublishSerializer
        def put(self, request,*args,**kwargs):
            return self.update(request,*args,**kwargs)  # update
    
        def delete(self, request,*args,**kwargs):
            return self.destroy(request,*args,**kwargs)  # destroy
    
        def get(self, request,*args,**kwargs):
            return self.retrieve(request,*args,**kwargs)  # retrieve
    
    # 极简版
    # class AuthorHandle(generics.ListCreateAPIView):  # 查询创建二合一
    #     queryset = models.Author.objects.all()
    #     serializer_class = AuthorSerializer
    # #
    # class SAuthorHandle(generics.RetrieveUpdateDestroyAPIView):  # 查单 更新 删除  三合一
    #     queryset = models.Author.objects.all()
    #     serializer_class = AuthorSerializer
    
    #终极版
    from rest_framework.viewsets import ModelViewSet
    class SAuthorHandle(ModelViewSet):  # 全合一
        queryset = models.Author.objects.all()
        serializer_class = AuthorSerializer
    
    -- all_serializer.py --
    from app01 import models
    from rest_framework import serializers
    from rest_framework.response import Response   # 响应
    from rest_framework.views import APIView
    
    class BookSerializer(serializers.ModelSerializer):   # create     update
        class Meta:
            model = models.Book
            fields = '__all__'
    
    class PublishSerializer(serializers.ModelSerializer):
        class Meta:
            model = models.Publish
            fields = '__all__'
    
    class AuthorSerializer(serializers.ModelSerializer):
        class Meta:
            model = models.Author
            fields = '__all__'
    
    -- urls.py --
    from django.conf.urls import url
    from django.contrib import admin
    from app01 import views
    
    urlpatterns = [
        url(r'^admin/', admin.site.urls),
    
        url(r'books/$', views.BookHandle.as_view()),  # 不带参数的
        url(r'books/(d+)/', views.SBookHandle.as_view()),  # 带参数的
    
        url(r'publishs/$', views.PublishHandle.as_view()),  # 不带参数的
        url(r'publishs/(?P<pk>d+)/', views.SPublishHandle.as_view()),  # 不带参数的
    
        url(r'authors/$', views.SAuthorHandle.as_view({'get':'list','post':'create'})),  # 不带参数的
        url(r'authors/(?P<pk>d+)/', views.SAuthorHandle.as_view({'get':'retrieve','put':'update','delete':'destroy'})),  # 不带参数的
    ]
  • 相关阅读:
    RGB888转RGB666
    bmp文件格式详细解析
    Qt 5简介
    IntelliJ IDEA 快捷键
    猫猫学iOS之小知识之_xcode插件的删除方法_自己主动提示图片插件KSImageNamed有时不灵_分类或宏之类不能自己主动提示,
    sql server 2008出现远程过程调用失败
    Oracle-31-对视图DML操作
    uva 11127(暴力)
    各种排序算法的分析与实现
    我的Android进阶之旅------&gt;怎样解决Android 5.0中出现的警告: Service Intent must be explicit:
  • 原文地址:https://www.cnblogs.com/zhangchen-sx/p/10739358.html
Copyright © 2011-2022 走看看