如何继承基于类的视图的DRF权限?

时间:2018-08-21 22:55:36

标签: python django model-view-controller permissions

class ViewTwo(APIView):
    permission_classes = (permissions.IsBar,)

class ViewThree(ViewTwo):
    permission_classes = (permissions.IsXYZ,)

ViewThree如何继承ViewTwo权限才能同时拥有permissions.IsBarpermissions.IsXYZ权限类?

2 个答案:

答案 0 :(得分:2)

我想,您需要在get_permissions中实现ViewThree方法,该方法实例化并返回此视图所需的权限列表:

class ViewThree(ViewTwo):
    def get_permissions(self):
        return super().get_permissions() + (permissions.IsXYZ,)

答案 1 :(得分:0)

通过追加ViewTwo的权限来简单地定义ViewThree的权限。

class ViewTwo(TemplateView):
    permission_classes = (permissions.IsBar,)

class ViewThree(ViewTwo):
    permission_classes = ViewTwo.permission_classes + (permissions.IsXYZ,)