Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.'
like image 642
henry12 Avatar asked Apr 08 '14 19:04

henry12


1 Answers

This is because you removed the permission_classes. You can use permission_classes = (IsAuthenticatedOrReadOnly,)

like image 192
Leonid Avatar answered Nov 01 '22 21:11

Leonid