Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading