基于 class 的视图中的方法装饰器不工作
Method Decorator in class based view is not working
我正在使用基于 class 的视图并仅在使用方法装饰器的 POST 方法上应用权限 classes。直到昨天它还在工作,但突然间停止了。我找不到问题。
class OfferCreateListView(ListCreateAPIView):
serializer_class = OfferSerializer
queryset = Offers.objects.filter(user__isnull=True)
@method_decorator(permission_classes((IsAuthenticated,)))
@method_decorator(authentication_classes((BasicAuthentication, SessionAuthentication, TokenAuthentication,)))
def post(self, request, *args, **kwargs):
return super(OfferCreateListView, self).post(request, *args, **kwargs)
我哪里做错了。有什么设置可以让它工作吗??
permission_classes
和 authentication_classes
装饰器专为基于函数的视图而设计。我并没有完全遵循其余框架代码,但令我惊讶的是它直到昨天才起作用——我不打算将装饰器与基于 class 的视图一起使用。
而是在 class 上设置属性。由于您只想将权限 class 应用于 post 请求,因此听起来您想要 IsAuthenticatedOrReadOnly
.
class OfferCreateListView(ListCreateAPIView):
permission_classes = (IsAuthenticatedOrReadOnly,)
authentication_classes = (BasicAuthentication, SessionAuthentication, TokenAuthentication,)
serializer_class = OfferSerializer
queryset = Offers.objects.filter(user__isnull=True)
我正在使用基于 class 的视图并仅在使用方法装饰器的 POST 方法上应用权限 classes。直到昨天它还在工作,但突然间停止了。我找不到问题。
class OfferCreateListView(ListCreateAPIView):
serializer_class = OfferSerializer
queryset = Offers.objects.filter(user__isnull=True)
@method_decorator(permission_classes((IsAuthenticated,)))
@method_decorator(authentication_classes((BasicAuthentication, SessionAuthentication, TokenAuthentication,)))
def post(self, request, *args, **kwargs):
return super(OfferCreateListView, self).post(request, *args, **kwargs)
我哪里做错了。有什么设置可以让它工作吗??
permission_classes
和 authentication_classes
装饰器专为基于函数的视图而设计。我并没有完全遵循其余框架代码,但令我惊讶的是它直到昨天才起作用——我不打算将装饰器与基于 class 的视图一起使用。
而是在 class 上设置属性。由于您只想将权限 class 应用于 post 请求,因此听起来您想要 IsAuthenticatedOrReadOnly
.
class OfferCreateListView(ListCreateAPIView):
permission_classes = (IsAuthenticatedOrReadOnly,)
authentication_classes = (BasicAuthentication, SessionAuthentication, TokenAuthentication,)
serializer_class = OfferSerializer
queryset = Offers.objects.filter(user__isnull=True)