Skip to content

Instantly share code, notes, and snippets.

@andreagrandi
Created September 30, 2016 09:49
Show Gist options
  • Select an option

  • Save andreagrandi/14e07afd293fafaea770f69cf66cac14 to your computer and use it in GitHub Desktop.

Select an option

Save andreagrandi/14e07afd293fafaea770f69cf66cac14 to your computer and use it in GitHub Desktop.
IsAdminOrReadOnly is a custom Django Rest Framework permission class that allows Admin users to POST and anonymous to GET
from rest_framework.permissions import BasePermission, SAFE_METHODS
class IsAdminOrReadOnly(BasePermission):
def has_permission(self, request, view):
if request.method in SAFE_METHODS:
return True
else:
return request.user.is_staff
@edrisranjbar
Copy link

this code snippet was so helpful. Thanks a lot.

@Shndasd
Copy link

Shndasd commented Feb 9, 2026

`from rest_framework import permissions

class IsAdminOrReadOnly(permissions.BasePermission):
def has_permission(self, request, view):
return bool(request.method == 'GET' or (request.user and request.user.is_staff))`

you can use it too

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment