Advertisement
robertvari

Custom permission for post owners

Apr 19th, 2020
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.55 KB | None | 0 0
  1. from rest_framework import permissions
  2.  
  3.  
  4. class IsOwnerOrReadOnly(permissions.BasePermission):
  5.     """
  6.    Custom permission to only allow owners of an object to edit it.
  7.    """
  8.  
  9.     def has_object_permission(self, request, view, obj):
  10.         # Read permissions are allowed to any request,
  11.         # so we'll always allow GET, HEAD or OPTIONS requests.
  12.         if request.method in permissions.SAFE_METHODS:
  13.             return True
  14.  
  15.         # Write permissions are only allowed to the owner of the snippet.
  16.         return obj.author == request.user
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement