How to enforce user permissions using user Groups in Django Class Based Views

Viewed 257

In my Django app, I have created Groups and assigned (selectively) Permissions the Groups. And later assigned the Groups to the Users. These steps were carried out in the app's admin.

Now I am trying to restrict certain views to the users (using CBV) like as under:

class UomListView(LoginRequiredMixin, PermissionRequiredMixin, ListView):
    template_name = "..." 
    context_object_name = 'unit_meas'
    model = U_o_M

    permission_required = 'myapp.view_u_o_m'

    def get_queryset(self):
       return U_o_M.objects.order_by('uom')

As expected I am able to restrict access to the view to users who are assigned the permission "myApp.view_u_o_m".

My understanding of the system is that if a user is attached to a Group which has permission "view_u_o_m" should be automatically assigned the privilege of accessing the view.

To quote (from Admin > Change User page):

The groups this user belongs to. A user will get all permissions granted to each of their groups.

However, when I remove the line permission_required = 'myApp.view_u_o_m', anticipating that the user Permission will hold good but it doesn't and I get an error saying

View is missing the permission_required attribute. Define... or override .get_permission_required().

Obviously I am wrong about how defining "Groups" affect the Permissions scenario.

May I ask somebody to help clarify the issue here and how to use Groups to control access to views.

Thanks

0 Answers
Related