Cannot apply DjangoModelPermissions on a view that does not have `.queryset` property or overrides the `.get_queryset()` method
You probably have set DjangoModelPermissions
as a default permission class in your settings. Something like:
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.DjangoModelPermissions',
)
}
DjangoModelPermissions
can only be applied to views that have a .queryset
property or .get_queryset()
method.
Since Tutorial 2 uses FBVs, you probably need to convert it to a CBV or an easy way is to specify a different permission class for that view.
You must be using the api_view
decorator in your view. You can then define permissions
like below:
from rest_framework.decorators import api_view, permission_classes
from rest_framework import permissions
@api_view([..])
@permission_classes((permissions.AllowAny,))
def my_view(request)
...
To resolve the renderer error, you need to add the corresponding renderer to your settings.
REST_FRAMEWORK = {
'DEFAULT_RENDERER_CLASSES': (
'rest_framework.renderers.<corresponding_renderer>',
...
)
}
I got it working in another way. My logged in user was the superuser which i have created. So i have created another user from admin and made him staff user and provided all the permissions. Then logged in to admin by that user.
In settings.py file i changed code.
REST_FRAMEWORK = {
# Use Django's standard `django.contrib.auth` permissions,
# or allow read-only access for unauthenticated users.
'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.IsAuthenticated',
]
}
And it worked.
Solution for me was as pointed out by @ProfNandaa above
Quick fix, comment out the 'rest_framework.renders.DjangoModelPermissions' line for now -- if you are following the DRF Tutorial 2; and perhaps you had added that in settings.py during the homepage example.
I had indeed added this from the homepage example before embarking on the tutorial and hit the same issue.
When I commented out the offending code
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.DjangoModelPermissions',
)
}
from settings.py it all worked fine again.