Skip to content
Advertisement

How to get the param PK in django restframework permission?

I have this permission in Django that look like this:

class PermissionName(BasePermission):
    

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

        if request.method in SAFE_METHODS:
            return True
        #I want to extract the pk or id params in here.

        return False

Can anybody help me get the params of request I tried using self.kwargs["pk"], request.POST['pk'] but they all return saying object has no attribute.

Advertisement

Answer

If pk is a parameter of the POST request, you can access it with request.POST.get('pk'). In your post, you are not using get(), so that could be the issue.

If the object is provided as an argument to the function, I’m assuming you want the ID of it. That can also be achieved with obj.id.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement