sdu项目实训3-2 DRF中二级视图和Mixin类配合使用

Mixin类提供用于提供基本视图行为(列表视图, 详情视图)的操作;
它是配合二级视图GenericAPIView使用的


前言

常用的Mixin类及方法:

    类名称                 提供方法        功能
    ListModelMixin        list          查询所有的数据
    CreateModelMixin      create        创建单个对象
    RetrieveModelMixin    retrieve      获取单个对象
    UpdateModelMixin      update        更新单个对象
    DestroyModelMixin     destroy       删除单个对象

可以使用Mixin和二级视图GenericAPIView对列表视图和详情视图做改写


下面案例可供参考

一、子路由

子路由配置如下:

urlpatterns = [

    path('test', TestAPIView.as_view()),
    #’一级视图’子路由
    path('cropsa',ShowCropaAPIView.as_view()),
    path('newuser',CreateUserAPIView.as_view()),

    #二级视图子路由
    path('generic_cropsa',ShowCropaGenericAPIView.as_view()),
    path('generic_newuser',CreateUserGenericAPIView.as_view()),

    #二级视图和Mixin配合使用
    path('mixin_generic_cropsa',ShowCropaMixinGenericAPIView.as_view()),
    path('mixin_generic_newuser',CreateUserMixinGenericAPIView.as_view())
]

二、类视图

类视图改进如下:

from rest_framework.mixins import ListModelMixin,CreateModelMixin

class ShowCropaMixinGenericAPIView(GenericAPIView,ListModelMixin):

    # 提供公共的属性
    queryset = City_cropinfo.objects.all()
    serializer_class = City_cropinfoModelSerializer

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


class CreateUserMixinGenericAPIView(GenericAPIView,CreateModelMixin):

    # 提供公共的属性
    queryset = User.objects.all()
    serializer_class = UserModelSerializer

    def post(self,request):
        return self.create(request)

三、使用Postman进行测试

  • 查询城市农作物产量
    在这里插入图片描述

  • 创建新用户
    在这里插入图片描述
    在这里插入图片描述

四、三级视图

如果没有大量自定义的行为, 可以使用通用视图(三级视图)解决

1.常见的三级视图

    类名称                 父类              提供方法        作用
    CreateAPIView       GenericAPIView,   post           创建单个对象
                        CreateModelMixin
                        
    ListAPIView         GenericAPIView,    get            查询所有的数据
                        ListModelMixin
    
    RetrieveAPIView     GenericAPIView,    get            获取单个对象
                        RetrieveModelMixin 
                        
    DestroyAPIView      GenericAPIView,    delete         删除单个对象
                        DestroyModelMixin
                        
    UpdateAPIView       GenericAPIView,    put            更新单个对象
                        UpdateModelMixin             

2.子路由

urlpatterns = [

    #三级视图
    path('third_cropsa',ShowCropaThirdView.as_view()),
    path('third_newuser',CreateUserThirdView.as_view())
]

3.类视图

from rest_framework.generics import ListAPIView, CreateAPIView

class ShowCropaThirdView(ListAPIView):

    # 提供公共的属性
    queryset = City_cropinfo.objects.all()
    serializer_class = City_cropinfoModelSerializer


class CreateUserThirdView(CreateAPIView):

    # 提供公共的属性
    queryset = User.objects.all()
    serializer_class = UserModelSerializer

总结

使用了Mixin类之后,二级视图编写代码更加简洁,但是我认为相对来说局限性也更强了;
使用了三级视图之后,究极简化,大道至简,但是无法解决自定义的行为。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值