Reputation: 5452
I have a ModelViewSet
and I want it to be accessed only by authenticated users, so I added the permission_classes = (permissions.IsAuthenticated)
, but I get an error:
TypeError at /es/general/countries/
'type' object is not iterable
This is the ViewSet
:
class CountryViewSet(viewsets.ModelViewSet):
permission_classes = (permissions.IsAuthenticated)
queryset = models.Country.objects.all()
serializer_class = serializers.CountrySerializer
If I remove the IsAuthenticated
assignment line, the ViewSet works, but with it, I get the error.
I'll appreciate your help.
Upvotes: 0
Views: 1660
Reputation: 4653
You are missing one little comma:
permission_classes = (IsAuthenticated,)
# This works
for a in permission_classes:
print("fine")
permission_classes_error = (IsAuthenticated)
#TypeError: 'type' object is not iterable
for b in permission_classes_error:
print("fine")
With the comma, we are creating a new tuple (which is an iterable type) with just one element. Without it we are assigning IsAuthenticated
(which is of type object
and therefore not iterable) to permission_classes
.
Your fixed ViewSet
looks like this:
class CountryViewSet(viewsets.ModelViewSet):
permission_classes = (permissions.IsAuthenticated,)
queryset = models.Country.objects.all()
serializer_class = serializers.CountrySerializer
Upvotes: 1