ms_identity_web.login_required to a class-based view

@ms_identity_web.login_required
def index(request):
    print(request.identity_context_data.username)
    return render(request, "auth/status.html")

But how do I add @ms_identity_web.login_required to a class based view ?
(this is DRF JSON view)

@ms_identity_web.login_required
class ApplicationViewSet(viewsets.ModelViewSet):
    queryset = Application.objects.all().order_by('-version')
    serializer_class = ApplicationSerializer

throws :

extra_actions = viewset.get_extra_actions()

AttributeError: ‘function’ object has no attribute ‘get_extra_actions’

It seems you want to check if the user is authenticated or not, in this case you should use permission_classes in your ModelViewSet like this

from rest_framework import permissions

class ApplicationViewSet(viewsets.ModelViewSet):
    queryset = Application.objects.all().order_by('-version')
    serializer_class = ApplicationSerializer
    permission_classes = [permissions.IsAuthenticated]

and for further reference you can check this out Permissions - Django REST framework

How is this working ? I assumed permission_classes = [permissions.IsAuthenticated] was only applicable to Django’s default login and not a custom login like Azure AD.

The permission_classes with IsAuthenticated permission specifies if the user is logged in or not, If you want custom authentication then you can create custom permission class and can use it.

But right now adding permission_classes = [permissions.IsAuthenticated] it still didnt fetch when not logged in.

EDIT - sorry, I was logged in to admin. So I need to write custom permission_classes

hi! how you manage to use @ms_identity_web.login_required for you DRF??

Hi! I have came up with solution:

from django.utils.decorators import method_decorator

ms_identity_web = settings.MS_IDENTITY_WEB

@method_decorator(ms_identity_web.login_required, name=“dispatch”)
class DocumentTypeViewSet(viewsets.ModelViewSet):
queryset = DocumentType.objects.all()
serializer_class = DocumentTypeSerializer