AssertionError:无法对没有`.model`或`.queryset`属性的视图应用DjangoModelPermissions

时间:2014-04-08 19:39:42

标签: django permissions django-rest-framework

我以前在我的项目中有这个观点:

from rest_framework import status
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.parsers import JSONParser
from rest_framework.permissions import IsAuthenticated


from rest_api.my_app.serializer import MySerializer
from my_project.models import Bag


class MyView(APIView):
    parser_classes = (JSONParser,)
    queryset = Bag.objects.all()
    permission_classes = (IsAuthenticated,)


    @staticmethod
    def post(self, request, format=None):
        serializer = MySerializer(data=request.DATA)
        if serializer.is_valid():
            serializer.save(),
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

然而我后来意识到我不需要查询集,因此我删除了该行和 保留的权限和查询集:

from rest_framework import status
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.parsers import JSONParser


from rest_api.my_app.serializer import MySerializer


class MyView(APIView):
    parser_classes = (JSONParser,)
    @staticmethod
    def post(self, request, format=None):
        serializer = MySerializer(data=request.DATA)
        if serializer.is_valid():
            serializer.save(),
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

如果我尝试运行代码,我会收到以下错误消息:

AssertionError: Cannot apply DjangoModelPermissions on a view that does not have     `.model` or `.queryset` property.'

1 个答案:

答案 0 :(得分:9)

这是因为您删除了permission_classes。 您可以使用 permission_classes = (IsAuthenticatedOrReadOnly,)