我知道这晚了 - 但我最近不得不弄清楚同样的事情,并且在文档或互联网上几乎没有找到帮助。这就是我最终想出的——希望它可以帮助其他可能遇到同样问题的人:
from request_admin.models import Rep
from request_admin.serializers import RepSerializer
from rest_framework import generics
from rest_framework import permissions
from rest_framework import mixins
from django.core.exceptions import PermissionDenied
class RepList(mixins.ListModelMixin,
mixins.CreateModelMixin,
generics.GenericAPIView):
paginate_by = 10
queryset = Rep.objects.all()
serializer_class = RepSerializer
def get(self, request, *args, **kwargs):
return self.list(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
if request.user.is_authenticated() == False:
raise PermissionDenied
return self.create(request, *args, **kwargs)
这是用户对象的 django 文档
这是异常的 django 文档
这是mixins的 django 文档