S
S
slickstars2015-09-04 17:46:30
Django
slickstars, 2015-09-04 17:46:30

How to set permissions on ListCreateAPIView?

Hi I have a class:

class PostList(generics.ListCreateAPIView):
    permission_classes = (permissions.IsAuthenticated,)
    model = Post
    queryset = Post.objects.all()
    serializer_class = PostSerializer

With it, I can create and view all posts, and in urls I can write:
url(r'^posts/$', views.PostList.as_view(), name='post-list'),

My question is, how do the List and Create (get, post) actions set different permissions and at the same time retain the ability to access these actions from the same url (/posts) using different GET, POST methods?
Indeed, in PostList(generics.ListCreateAPIView): I can set permission_classes, but then the permission will apply to both List and Create actions

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
marazmiki, 2015-09-04
@slickstars

Like this:

class PostList(generics.ListCreateAPIView):
    def get_permissions(self):
        if self.request.method  == 'GET':
            perms = [GetPermission]
        else:
            perms = [NotGetPermission]
        return [p() for p in perms]
It is clear that the solution does not shine with beauty, it is, as they say, a low level. But this principle will be the basis of all improvements.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question