耿直boy6

AI-序列化-做五个数据接口


#
url.py

url(r'^customer/$', views.CustomerView.as_view()), #查询所有数据、添加数据接口url
url(r'^customer/(\d+)', views.SCustomerView.as_view()), #查询、更新、删除单个数据接口url
from app01.models import *
from rest_framework import serializers
from rest_framework.response import Response
class CustomerSerializer(serializers.ModelSerializer):
    class Meta:
        model=Customer
        # field=["name","gender"]
        fields="__all__"
    # courses=serializers.SerializerMethodField()
    # def get_courses(self,obj):
    #     ret=[]
    #     for i in obj.courses.all():
    #         ret.append({"name":"i.name"})
    #     return ret

#查所有的数据、添加数据接口 from rest_framework.views import APIView class CustomerView(APIView): def get(self,request): customer_list=Customer.objects.all() cs=CustomerSerializer(customer_list,many=True) return Response(cs.data) def post(self,request): cs=CustomerSerializer(data=request.data,many=False) if cs.is_valid(): cs.save() return Response(cs.data) else: return Response(cs.errors) #查询、删除、更新单个数据接口,新建类SCustomerView #当使用http://127.0.0.1:8055/customer/1访问时不报错,那是浏览器自动补全了"/",
在setting中添加APPEND_SLASH=False就不会加"/"
class SCustomerView(APIView): def get(self,request,id): edit_obj=Customer.objects.get(pk=id) cs=CustomerSerializer(edit_obj,many=False) #many=False 只查询一条数据 return Response(cs.data) def put(self,request,id): edit_obj=Customer.objects.get(pk=id) cs=CustomerSerializer(data=request.data,instance=edit_obj) #序列化一个对象使用instance=edit_obj,
                                          序列化列表直接写入列表即可
if cs.is_valid(): cs.save() # edit_obj.update(request.data) return Response(cs.data) else: return Response(cs.errors) def delete(self,request,id): eidt_obj=Customer.objects.get(pk=id).delete() return Response("") #按照restful规范,删除返回 "" 即可

 需求:当有很多表时,不可能一个一个的写增删改查,使用mixins解决了这个:

   mixins里封装了增删改查功能,但是需要继承APIView,APIView已经被封装在了generics里,调用Generics即可

 

序列化多表功能最终版代码

#url.py
    #序列化school
    url(r'^school/$', views.SchoolView.as_view()),
    url(r'^school/(?P<pk>\d+)', views.SSchoolView.as_view()),  #做单个数据差、更新、删时,要传入pk值,
                                       使用有名分组 (?P<pk>\d+)

 

#views.py
from
rest_framework.mixins import ListModelMixin,CreateModelMixin,UpdateModelMixin,
                                        DestroyModelMixin,RetrieveModelMixin
from rest_framework.generics import GenericAPIView class SchoolSerializer(serializers.ModelSerializer): #可以将这个组件封装到serializer.py文件,导入就行 class Meta: model=School fields="__all__" #做整体数据的查询接口、添加数据接口 class SchoolView(ListModelMixin,CreateModelMixin,GenericAPIView): queryset = School.objects.all() #只需要变动两个字段,qyeryset、序列化组件名儿,必须按照这种格式写! serializer_class = SchoolSerializer def get(self, request, *args, **kwargs): #点开ListModelMixin,有个list方法,里边的参数copy过来即可 return self.list(request, *args, **kwargs) #返回self.方法名儿() def post(self, request, *args, **kwargs): #点开CreateModelMixin,有个create方法,里边的参数copy过来即可 return self.create(request, *args, **kwargs) # 做单个数据的,查、删、更新接口 class SSchoolView(UpdateModelMixin,RetrieveModelMixin,DestroyModelMixin,GenericAPIView): queryset = School.objects.all() serializer_class = SchoolSerializer 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)

 

posted on 2018-12-04 16:38  猫巴  阅读(215)  评论(0编辑  收藏  举报

导航

原文:https://www.cnblogs.com/djfboai/ 版权声明:本文为博主原创文章,转载请附上博文链接!