I am using Django user_passes_test decorator to check the User Permission.
@user_passes_test(lambda u: has_add_permission(u, "project"))
def create_project(request):
......
I am calling a callback function has_add_permission which takes two arguments User and a String. I would like to pass the request object along with it is that possible? Also, can anyone please tell me how are we able to access the User object inside the decorator directly.
No, you cannot pass request to
user_passes_test. To understand why and how it works, just head over to the source:This is the code behind the
user_passes_testdecorator. As you can see, the test function passed to the decorator (in your case,lambda u: has_add_permission(u, "project")) is passed just one argument,request.user. Now, it’s of course possible to write your own decorator (even copying this code directly and just modifying it) to also pass therequestitself, but you can’t do it with the defaultuser_passes_testimplementation.