作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我之前对我的项目有这样的看法:
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/
我是一名优秀的程序员,十分优秀!