I previously had this view on my project:
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)
However I later realized that I did not need the queryset and so I deleted that line and the permission and queryset to remain with:
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)
If I try to run the code i get this error message:
AssertionError: Cannot apply DjangoModelPermissions on a view that does not have `.model` or `.queryset` property.'
This is because you removed the permission_classes.
You can use
permission_classes = (IsAuthenticatedOrReadOnly,)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With