[Solved]-How to create a custom mixin in django?

30👍

We can make a Mixin that just overrides the dispatch method, like:

class ProductExistsRequiredMixin:

    def dispatch(self, request, *args, **kwargs):
        if Product.objects.filter(pk=1, activate=True):
            return super().dispatch(request, *args, **kwargs)
        else:
            raise PermissionDenied

and then use it in a view like:

class MyCustomView(ProductExistsRequiredMixin, View):

    # ...

Leave a comment