ModelViewSet does not overwrite DEFAULT_PERMISSION_CLASSES'

Hello I'm working on making all urls requires user to be authenticate , but some urls i want them to be accessible by public, so i use permission_classes = [AllowAny] and authentication_classes = ([]) to overwrite default configurations it work in APIVIEW but not in viewsets.ModelViewSet why ?

settings.py

REST_FRAMEWORK = {
    'DEFAULT_PERMISSION_CLASSES': [
        'rest_framework.permissions.IsAuthenticated',
    ],
    'DEFAULT_FILTER_BACKENDS': [
        'django_filters.rest_framework.DjangoFilterBackend',
    ],
    'DEFAULT_AUTHENTICATION_CLASSES': [
        'rest_framework_simplejwt.authentication.JWTAuthentication',
    ],
    'DATETIME_FORMAT': "%Y-%m-%d %H:%M:%S",
}

views.py

class ToolsListViewSet(viewsets.ModelViewSet):
    serializer_class = ToolsListSerializer
    permission_classes = [AllowAny]
    authentication_classes = ([])
    pagination_class = None
    def get_queryset(self):
        return Tools.objects.filter(is_active=True)
    enter code here

error

{
    "detail": "Authentication credentials were not provided."
}

I checked your code, but can't find any problem.

Here is class inheritance in Django.

ModelViewSet <- GenericViewSet <- GenericAPIView <- APIView

So if it works with APIView, it should work with ModelViewSet too.

Вернуться на верх