Search code examples
djangotastypie

Get kwargs in Tastypie custom authorization


I would like to get kwargs in Tastypie custom authorization. I've to authorize whether user has access to the id in URL kwargs.

Authorization methods doesn't seem to pass kwargs but passes only bundle and object_list.


Solution

  • As you said, a custom Authorization does not has **kwargs in the signature. But you can access URL parameters (like id) using bundle.request.

    This kind of example bellow should work:

    class RestrictedIdAuthorization(Authorization):
        def read_detail(self, object_list, bundle):
            param_id = bundle.request.GET['id']
            accepted_ids = [42, 54, 67] # Must be changed, of course.
            return param_id in accepted_ids 
    

    You can take a look to this post to have another example.