DRF的权限认证逻辑

DRF中新构造的View类的继承一览图,图片引用:https://www.cnblogs.com/hehecat/p/9349427.html
在这里插入图片描述
以下是各类的大概说明:

class APIView(View):
	"""
	推荐!
	干净的APIView
	"""

class GenericAPIView(views.APIView):
    """
    加入了get_object,序列化等
    Base class for all other generic views.
    """

class ViewSet(ViewSetMixin, views.APIView):
    """
    只重写了as_vieww
    The base ViewSet class does not provide any actions by default.
    """
    pass

class GenericViewSet(ViewSetMixin, generics.GenericAPIView):
    """
    没有提供action,但是加入了get_object,序列化等,另外重写了as_vieww
    The GenericViewSet class does not provide any actions by default,
    but does include the base set of generic view behavior, such as
    the `get_object` and `get_queryset` methods.
    """
    pass

最终的封装类ModelViewSet,已经抽象到直接实现了rest api的基本增删改查操作,集成度非常高,路由也比较方便,可以直接注册路由,可以自己编写。

router.register(r'groups', views.GroupViewSet)

# 但因为继承了ViewSetMixin重写了as_view方法,如果自己写路由这时候需要加入action参数。
urlpatterns = [
    path('sessions/', views.UserViewSet.as_view({'get': 'list'}))
]

但是

一般情况下,过于抽象的东西可扩展性是很差的,我们为了实现自己想实现的方式,继承APIView类自由开发才是一个灵活的开发方式。

简单看了下逻辑,画了DRF的权限和认证代码逻辑图如下:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值