博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
rest-framework之视图
阅读量:4950 次
发布时间:2019-06-11

本文共 4383 字,大约阅读时间需要 14 分钟。

 

视图

使用混合(mixins)

上一节的视图部分:

from rest_framework.views import APIViewfrom rest_framework.response import Responsefrom .models import *from django.shortcuts import HttpResponsefrom django.core import serializersfrom rest_framework import serializersclass BookSerializers(serializers.ModelSerializer):      class Meta:          model=Book          fields="__all__"          #depth=1class PublshSerializers(serializers.ModelSerializer):      class Meta:          model=Publish          fields="__all__"          depth=1class BookViewSet(APIView):    def get(self,request,*args,**kwargs):        book_list=Book.objects.all()        bs=BookSerializers(book_list,many=True,context={
'request': request}) return Response(bs.data) def post(self,request,*args,**kwargs): print(request.data) bs=BookSerializers(data=request.data,many=False) if bs.is_valid(): print(bs.validated_data) bs.save() return Response(bs.data) else: return HttpResponse(bs.errors)class BookDetailViewSet(APIView): def get(self,request,pk): book_obj=Book.objects.filter(pk=pk).first() bs=BookSerializers(book_obj,context={
'request': request}) return Response(bs.data) def put(self,request,pk): book_obj=Book.objects.filter(pk=pk).first() bs=BookSerializers(book_obj,data=request.data,context={
'request': request}) if bs.is_valid(): bs.save() return Response(bs.data) else: return HttpResponse(bs.errors)class PublishViewSet(APIView): def get(self,request,*args,**kwargs): publish_list=Publish.objects.all() bs=PublshSerializers(publish_list,many=True,context={
'request': request}) return Response(bs.data) def post(self,request,*args,**kwargs): bs=PublshSerializers(data=request.data,many=False) if bs.is_valid(): # print(bs.validated_data) bs.save() return Response(bs.data) else: return HttpResponse(bs.errors)class PublishDetailViewSet(APIView): def get(self,request,pk): publish_obj=Publish.objects.filter(pk=pk).first() bs=PublshSerializers(publish_obj,context={
'request': request}) return Response(bs.data) def put(self,request,pk): publish_obj=Publish.objects.filter(pk=pk).first() bs=PublshSerializers(publish_obj,data=request.data,context={
'request': request}) if bs.is_valid(): bs.save() return Response(bs.data) else: return HttpResponse(bs.errors)

通过使用mixin类编写视图:

from rest_framework import mixinsfrom rest_framework import genericsclass BookViewSet(mixins.ListModelMixin,                  mixins.CreateModelMixin,                  generics.GenericAPIView):    queryset = Book.objects.all()    serializer_class = BookSerializers    def get(self, request, *args, **kwargs):        return self.list(request, *args, **kwargs)    def post(self, request, *args, **kwargs):        return self.create(request, *args, **kwargs)class BookDetailViewSet(mixins.RetrieveModelMixin,                    mixins.UpdateModelMixin,                    mixins.DestroyModelMixin,                    generics.GenericAPIView):    queryset = Book.objects.all()    serializer_class = BookSerializers    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)

使用通用的基于类的视图

通过使用mixin类,我们使用更少的代码重写了这些视图,但我们还可以再进一步。REST框架提供了一组已经混合好(mixed-in)的通用视图,我们可以使用它来简化我们的views.py模块。

from rest_framework import mixinsfrom rest_framework import genericsclass BookViewSet(generics.ListCreateAPIView):    queryset = Book.objects.all()    serializer_class = BookSerializersclass BookDetailViewSet(generics.RetrieveUpdateDestroyAPIView):    queryset = Book.objects.all()    serializer_class = BookSerializersclass PublishViewSet(generics.ListCreateAPIView):    queryset = Publish.objects.all()    serializer_class = PublshSerializersclass PublishDetailViewSet(generics.RetrieveUpdateDestroyAPIView):    queryset = Publish.objects.all()    serializer_class = PublshSerializers

 

转载于:https://www.cnblogs.com/l-jie-n/p/9866438.html

你可能感兴趣的文章
sqlite
查看>>
机电行业如何进行信息化建设
查看>>
Windows Azure Platform Introduction (4) Windows Azure架构
查看>>
【转】chrome developer tool 调试技巧
查看>>
mahout运行测试与kmeans算法解析
查看>>
互相给一巴掌器
查看>>
Android SDK环境变量配置
查看>>
VM10虚拟机安装图解
查看>>
9、总线
查看>>
Git 笔记 - section 1
查看>>
java通过jsp+javaBean+servlet实现下载功能
查看>>
STM32 使用Cubemx 建一个USB(HID)设备下位机,实现数据收发
查看>>
异步表单提交
查看>>
[洛谷U871]building
查看>>
次小生成树
查看>>
Redis在windows下安装过程
查看>>
ip转城市接口,ip转省份接口,ip转城市PHP方法
查看>>
android 注释常用标签
查看>>
Spring context:property-placeholder 一些坑
查看>>
如何使用 adb 命令实现自动化测试
查看>>