0

Yesterday I had a Stackoverflow question about putting a permissioning check on a class-based-view. The solution seem(s) to be incorporating a PermissionDeniedMixin. It also looks like I could try UserPassesTestMixin from Django-braces.

This made sense, but I was doing some background reading on the dispatch() method and stumbled onto this part of the documentation:

To decorate every instance of a class-based view, you need to decorate the class definition itself. To do this you apply the decorator to the dispatch() method of the class.

Why would I need or choose to decorate the instances with the permission mixins rather than the class itself?

user3556757
  • 3,469
  • 4
  • 30
  • 70

1 Answers1

0

The main reason to write a mixin instead of a decorator is that mixins are classes and therefore extensible. If I want to slightly modify the behavior of LoginRequiredMixin.handle_no_permission, for example, it's easy enough to write a new class that extends LoginRequiredMixin, override handle_no_permission, and use that modified class instead. Being a function, the login_required decorator is not as straightforward to customize.

mindcruzer
  • 834
  • 5
  • 9