request.user
and request.auth
properties to determine if the incoming request should be permitted.IsAuthenticated
class in REST framework.IsAuthenticatedOrReadOnly
class in REST framework.exceptions.PermissionDenied
or exceptions.NotAuthenticated
exception will be raised, and the main body of the view will not run.WWW-Authenticate
headers. — An HTTP 403 Forbidden response will be returned.WWW-Authenticate headers
. — An HTTP 401 Unauthorized response, with an appropriate WWW-Authenticate
header will be returned..get_object()
is called. As with view level permissions, an exceptions.PermissionDenied
exception will be raised if the user is not allowed to act on the given object.get_object
method on a generic view, then you’ll need to explicitly call the .check_object_permissions(request, obj)
method on the view at the point at which you’ve retrieved the object.This will either raise a PermissionDenied
or NotAuthenticated
exception, or simply return if the view has the appropriate permissions. For example:
def get_object(self):
obj = get_object_or_404(self.get_queryset(), pk=self.kwargs["pk"])
self.check_object_permissions(self.request, obj)
return obj
DjangoObjectPermissions
, the provided permission classes in rest_framework.permissions
do not implement the methods necessary to check object permissions. If you wish to use the provided permission classes in order to check object permissions, you must subclass them and implement the has_object_permission()
method.The default permission policy may be set globally, using the DEFAULT_PERMISSION_CLASSES
setting. For example:
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.IsAuthenticated',
]
}
If not specified, this setting defaults to allowing unrestricted access:
'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.AllowAny',
]
AllowAny
permission class will allow unrestricted access, regardless of if the request was authenticated or unauthenticated.IsAuthenticated
permission class will deny permission to any unauthenticated user, and allow permission otherwise.IsAdminUser
permission class will deny permission to any user, unless user.is_staff
is True
in which case permission will be allowed.IsAuthenticatedOrReadOnly
will allow authenticated users to perform any request. Requests for unauthorised users will only be permitted if the request method is one of the “safe” methods; GET
, HEAD
or OPTIONS
.django.contrib.auth
model permissions. This permission must only be applied to views that have a .queryset
property or get_queryset()
method. Authorization will only be granted if the user is authenticated and has the relevant model permissions assigned. The appropriate model is determined by checking get_queryset().model
or queryset.model
.
POST
requests require the user to have the add
permission on the model.PUT
and PATCH
requests require the user to have the change
permission on the model.DELETE
requests require the user to have the delete
permission on the model.view
model permission for GET
requests.DjangoModelPermissions
and set the .perms_map
property. Refer to the source code for details.DjangoModelPermissions
, but also allows unauthenticated users to have read-only access to the API.DjangoModelPermissions
, this permission must only be applied to views that have a .queryset
property or .get_queryset()
method. Authorization will only be granted if the user is authenticated and has the relevant per-object permissions and relevant model permissions assigned.
POST
requests require the user to have the add
permission on the model instance.PUT
and PATCH
requests require the user to have the change
permission on the model instance.DELETE
requests require the user to have the delete
permission on the model instance.DjangoObjectPermissions
does not require the django-guardian package, and should support other object-level backends equally well.DjangoObjectPermissions
and setting the .perms_map
property.view
permissions for GET
, HEAD
and OPTIONS
requests and are using django-guardian for your object-level permissions backend, you’ll want to consider using the DjangoObjectPermissionsFilter
class provided by the djangorestframework-guardian
package. It ensures that list endpoints only return results including objects for which the user has appropriate view permissions.Source: https://www.django-rest-framework.org/api-guide/permissions/