我正在使用带有 ModelViewSets 的 drf-nested-routers。一切正常,但是,没有执行对资源/对象的权限检查。
处理单个资源/非嵌套 url 时,会执行权限检查。
有什么我想念的吗?
class CommentViewSet(viewsets.ModelViewSet):
permission_classes = [IsAuthenticated,
permissions.CanCreateEditViewDeleteComment]
def get_serializer_class(self, *args, **kwargs):
return CommentSerializer
def list(self, request, article_pk=None):
queryset = Comment.objects.select_related('article','user').filter(article=article_pk).prefetch_related('likes')
page = self.paginate_queryset(queryset)
if page is not None:
serializer = self.get_serializer(page, many=True)
return self.get_paginated_response(serializer.data)
serializer = self.get_serializer(queryset, many=True)
return Response(serializer.data)
def retrieve(self, request, pk=None, article_pk=None):
queryset = Comment.objects.select_related('article', 'user').filter(pk=pk, article=article_pk).prefetch_related(
'likes')
comment = get_object_or_404(queryset, pk=pk)
serializer = self.get_serializer(comment)
return Response(serializer.data)