Search code examples
pythondjangodjango-rest-framework

Should authentication_classes and permission_classes in Django REST Framework Views Be Defined with Lists or Tuples?


I'm trying to understand the best practice for setting authentication_classes and permission_classes in Django REST Framework's APIView. Specifically, I’ve seen both tuples and lists being used to define these attributes:

Using a tuple:

class Home(APIView):
    authentication_classes = (JWTAuthentication,)
    permission_classes = (permissions.IsAuthenticated,)

Using a list:

class Home(APIView):
    authentication_classes = [JWTAuthentication]
    permission_classes = [permissions.IsAuthenticated]

Both approaches seem to work correctly, but I'm unsure if there are any specific reasons to prefer one over the other. Should I use a list or a tuple in this scenario? Are there any implications for using one over the other in terms of performance, readability, or following Django REST Framework's best practices?

I tried using both tuples and lists for authentication_classes and permission_classes in Django REST Framework’s APIView. Both work fine, but I’m unsure which one is better or recommended. I was expecting to find clear guidance on this.


Solution

  • Both work equivalently. The Django REST Framework documentation seems to prefer lists, so maybe go with lists.

    class ListUsers(APIView):
        authentication_classes = [authentication.TokenAuthentication]
        permission_classes = [permissions.IsAdminUser]