Open Permission access custom for Django Rest Framework

Open Permission access custom for Django Rest Framework

When we need to open access for API Django-restframework, we can do it by extending the permission class.
Here is some example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class PublicUserPermissions(permissions.BasePermission):
    """
    Granting public access permissions
    """

    def has_object_permission(self, request, view, obj):

        # Allow get requests for all
        if request.method == 'GET':
            return True
        else:
            return request.user == obj


class SomeViewSet(viewsets.ModelViewSet):
    """
    API to view Some dataset
    """
    model = Some
    queryset = model.objects.all()
    permission_classes = (PublicUserPermissions, )

Leave a Reply

Your email address will not be published. Required fields are marked *