简体   繁体   English

如何为 DRF 中的方法设置不同的权限类

[英]How to have different permission classes for methods in DRF

I have a view similar to this:我有一个类似的观点:

from django.http import HttpResponse
from rest_framework import generics

class MyView(generics.ListCreateAPIView):

    def get_queryset(self):
        # <view logic>
        return HttpResponse('result')

    def post(self, request):
        # <view logic x2>
        return HttpResponse('message_post_template')

And I would like the GET request to have the permission class of IsAuthenticated and the POST request should have a permission class of HasAPIKey from Django REST Framework API Key .我希望 GET 请求具有IsAuthenticated的权限类,POST 请求应该具有来自Django REST Framework API KeyHasAPIKey的权限类。 How can I do this?我怎样才能做到这一点?

I tried doing permission_classes = [IsAuthenticated | HasAPIKey]我试过做permission_classes = [IsAuthenticated | HasAPIKey] permission_classes = [IsAuthenticated | HasAPIKey] but that would be too lenient because it would allow the functions to work if the other permission other than the one required is available. permission_classes = [IsAuthenticated | HasAPIKey]但这太宽松了,因为如果除所需权限之外的其他权限可用,它会允许函数工作。

from django.http import HttpResponse
from rest_framework import generics

class MyView(generics.ListCreateAPIView):

    def get_permissions(self):
        method = self.request.method
        if method == 'POST':
           return [HasAPIKey()]
        else:
           return [IsAuthenticated()]

    def get_queryset(self):
        # <view logic>
        return HttpResponse('result')

    def post(self, request):
        # <view logic x2>
        return HttpResponse('message_post_template')

Reference:参考:
https://github.com/encode/django-rest-framework/blob/dff9759555eefef67c552f175d04bb7d8381e919/rest_framework/views.py#L274 https://github.com/encode/django-rest-framework/blob/dff9759555eefef67c552f175d04bb7d8381e919/rest_framework/views.py#L274

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 DRF 的 DEFAULT_PERMISSION_CLASSES 中使用逻辑运算符? - How to use logical operators in DRF's DEFAULT_PERMISSION_CLASSES? 如何在 DRF ModelViewSet 中为单独的请求方法设置权限? - How can I set permission for seperate request methods in DRF ModelViewSet? Django DRF:@permission_classes 不起作用 - Django DRF: @permission_classes not working 如何创建具有相同路径但不同 HTTP 方法的 2 个动作 DRF - How to create 2 actions with same path but different HTTP methods DRF Django DRF @permission_classes 不适用于 IsAdminUser 权限 - Django DRF @permission_classes not working for IsAdminUser permission 在 drf 中的多个自定义权限类之间传递变量 - pass a variable between multiple custom permission classes in drf 如何在 DRF 中有效地检查 object 级别权限? - How to check object level permission efficiently in DRF? Python如何列出已导入到类中的所有方法/类 - Python How to list all methods/classes that have been imported into a class 如何在 Python 中使用相同的方法重构不同的类? - How do I refactor different classes with the same methods in Python? 如何在 Python 的一个字典中拥有来自不同类的不同对象? - How to have different objects from different classes in one dict in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM