gpt4 book ai didi

django - 断言错误: Cannot apply DjangoModelPermissions on a view that does not have `.model` or `.queryset` property

转载 作者:行者123 更新时间:2023-12-02 08:17:03 25 4
gpt4 key购买 nike

我之前对我的项目有这样的看法:

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.'

最佳答案

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

关于django - 断言错误: Cannot apply DjangoModelPermissions on a view that does not have `.model` or `.queryset` property,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22946635/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com