[SOLVED] Django PermissionRequiredMixin not working

Issue

This Content is from Stack Overflow. Question asked by Tanne

When I place the PermissionRequiredMixin as the most left paramenter, my requests get forwarded to the login URL even though the request is coming from an already authenticated user.

class ExampleViewSet(PermissionRequiredMixin, viewsets.ModelViewSet):
    permission_required = ('example.example_view',)

When I place the PermissionRequiredMixin after the ModelViewSet the authenticated user is detected, however, the permission_required is ignored, and every user without the permission is allowed as well. And this answer suggested, that this is caused by the placement of the parameter, which leads to the first problem.

class ExampleViewSet(viewsets.ModelViewSet, PermissionRequiredMixin):
    permission_required = ('example.example_view',)

How do I solve this problem?



Solution

It is not a problem. Order of inheritance classes is important. View base class have to set at last position. Mixins by theirs positions can override some function of the django view. The ordering of overriding function by the child class is in part defined by this ordering. First parent in the order will be called at first.
In your case, if you put a breakpoint in your PermissionRequiredMixin, you will see that python does not pass in it when you call your page

You can read some links as:


This Question was asked in StackOverflow by Tanne and Answered by Lucas Grugru It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?