set custom permission to model in Django
You can define your custom permission in DRF like this:
permissions.py:
from rest_framework.permissions import BasePermission
class IsOwner(BasePermission):
"""
Allows access only to owners
"""
def has_permission(self, request, view):
pk = request.GET.get('pk')
user_meta = UsersMeta.objects.filter(user=request.user, token=pk).first()
return True if user_meta and user_meta.type == "owner" else False
Usage:
from rest_framework.views import APIView
from rest_framework.response import Response
from .permissions import IsOwner
class YourView(APIView):
permission_classes = (IsOwner,)
def get(self, request, format=None):
content = {
'status': 'owner users'
}
return Response(content)