圆月山庄资源网 Design By www.vgjia.com

基础视图

示例环境搭建:新建一个Django项目,连接Mysql数据库,配置路由、视图函数、序列化单独创建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'^PublishView/', views.PublishView.as_view()),
 url(r'^PublishDetailView/("htmlcode">
# 视图:

from django.shortcuts import render
from rest_framework.views import APIView
from app01 import models
from app01.MySer import BookSer,PublishSer,AuthorSer
from rest_framework.response import Response
# Create your views here.
# 基本视图

class PublishView(APIView):

 def get(self,request):
  publish_list = models.Publish.objects.all()
  bs = PublishSer(publish_list, many=True)
  return Response(bs.data)

 def post(self, request):
  bs = PublishSer(data=request.data)
  if bs.is_valid():
   bs.save()
   return Response(bs.data)
  else:
   return Response(bs.errors)

class PublishDetailView(APIView):
 def get(self, request, pk):
  publish_obj = models.Publish.objects.filter(pk=pk).first()
  bs = PublishSer(publish_obj, many=False)
  return Response(bs.data)

 def put(self, request, pk):
  publish_obj = models.Publish.objects.filter(pk=pk).first()
  bs = PublishSer(data=request.data, instance=publish_obj)
  if bs.is_valid():
   bs.save()
   return Response(bs.data)
  else:
   return Response(bs.data)

 def delete(self, request, pk):
  models.Publish.objects.filter(pk=pk).delete()
  return Response("")
# MySer.py

from rest_framework import serializers
from app01 import models

class BookSer(serializers.ModelSerializer):
 class Meta:
  model = models.Book
  fields = '__all__'


class PublishSer(serializers.ModelSerializer):
 class Meta:
  model = models.Publish
  fields = '__all__'


class AuthorSer(serializers.ModelSerializer):
 class Meta:
  model = models.Author
  fields = '__all__'

基于mixins来封装的视图

其它不变,更改视图:

# 基于mixins来封装的视图
from rest_framework.mixins import CreateModelMixin,         ListModelMixin,         RetrieveModelMixin,         DestroyModelMixin,         UpdateModelMixin
from rest_framework.generics import GenericAPIView

class PublishView(CreateModelMixin, ListModelMixin, GenericAPIView):
 queryset = models.Publish.objects.all()
 serializer_class = PublishSer
 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 PublishDetailView(RetrieveModelMixin, UpdateModelMixin, DestroyModelMixin, GenericAPIView):
 queryset = models.Publish.objects.all()
 serializer_class = PublishSer
 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)

可以看出视图类中还是有很多冗余代码

mixins封装再封装,第三种方法

# 第三种写法:
from rest_framework.generics import CreateAPIView,         ListCreateAPIView,         DestroyAPIView,         RetrieveUpdateDestroyAPIView

class PublishView(ListCreateAPIView):
 queryset = models.Publish.objects.all()
 serializer_class = PublishSer

class PublishDetailView(RetrieveUpdateDestroyAPIView):
 queryset = models.Publish.objects.all()
 serializer_class = PublishSer

还是有冗余代码

第四种写法,再次封装,全部写在一个类中

# 路由
from django.conf.urls import url
from django.contrib import admin
from app01 import views

urlpatterns = [
 url(r'^admin/', admin.site.urls),

 url(r'^publish/$', views.PublishView.as_view({'get': 'list', 'post': 'create'})),
 url(r'^publish/("htmlcode">
# 第四种写法:5个接口写在一个类中
from rest_framework.viewsets import ModelViewSet
class PublishView(ModelViewSet):
 queryset = models.Publish.objects.all()
 serializer_class = PublishSer

补充:

from rest_framework.viewsets import ViewSetMixin
from rest_framework.views import APIView
# ViewSetMixin 重写了as_view方法
class Test(ViewSetMixin,APIView):

 def aaa(self,request):
  return Response()

以上这篇Django框架之DRF 基于mixins来封装的视图详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

标签:
Django,DRF,mixins,封装,视图

圆月山庄资源网 Design By www.vgjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
圆月山庄资源网 Design By www.vgjia.com

稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!

昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。

这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。

而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?