1

I'm trying to GET fetch my REST API in DRF, but keep getting the error below. What could be the problem? Thanks!


Internal Server Error: /polls/
.....
.....
TypeError: 'type' object is not iterable
[08/Aug/2021 07:02:50] "GET /polls/ HTTP/1.1" 500 100344
Not Found: /favicon.ico
[08/Aug/2021 07:02:53] "GET /favicon.ico HTTP/1.1" 404 3415

The apiviews code: apiviews.py


class PollList(generics.ListCreateAPIView):
    queryset = Poll.objects.all()
    serializer_class = PollSerializer

The polls code: models.py


class Poll(models.Model):
    question = models.CharField(max_length=100)
    created_by = models.ForeignKey(User, on_delete=models.CASCADE)
    pub_date = models.DateTimeField(auto_now=True)

Serializer.py:


class PollSerializer(serializers.ModelSerializer):
    choices = ChoiceSerializer(many=True, read_only=True, required=False)
    
    class Meta:
        model = Poll
        fields = '__all__'

urls.py:


urlpatterns = [
    path("polls/", PollList.as_view(), name="polls_list"),
]

Admin URLs:


urlpatterns = [
    path('admin/', admin.site.urls),
    re_path(r'^', include('vote.urls')),
]

Here's portions of the traceback:


Internal Server Error: /polls/
Traceback (most recent call last):
  File "D:\Studio\Python\REST\elections\env\lib\site-packages\django\core\handlers\exception.py", line 35, in inner 
....
....
\rest_framework\views.py", line 400, in initial        
    self.check_permissions(request)
  File "D:\Studio\Python\REST\elections\env\lib\site-packages\rest_framework\views.py", line 332, in check_permissions
    for permission in self.get_permissions():
  File "D:\Studio\Python\REST\elections\env\lib\site-packages\rest_framework\views.py", line 279, in get_permissions    return [permission() for permission in self.permission_classes]
TypeError: 'type' object is not iterable

Here's the Settings.py code implementing the Authentication:


REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.TokenAuthentication',
        'rest_framework.authentication.SessionAuthentication'
    ),
    'DEFAULT_PERMISSION_CLASSES': (
        'rest_framework.permissions.IsAuthenticated'
    )
}

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    ....
    ....
]

6
  • Please share the serializer and url as well. Commented Aug 8, 2021 at 5:16
  • I've updated above. Added the urls.py and serializer.py. Commented Aug 8, 2021 at 6:51
  • please share full stack strace Commented Aug 8, 2021 at 7:06
  • I've added portions of the traceback and admin URLs. Commented Aug 8, 2021 at 8:48
  • Do you have default permissions in your settings.py? Commented Aug 8, 2021 at 8:50

1 Answer 1

2

There is a missing comma in the DEFAULT_PERMISSION_CLASSES tuple, which effectively makes it not a tuple, but a single scalar value.

'DEFAULT_PERMISSION_CLASSES': (
    'rest_framework.permissions.IsAuthenticated', # !!!!
)
Sign up to request clarification or add additional context in comments.

1 Comment

WOOOOOW!!! YOU'RE THE BEST. WORKS LIKE MAGIC. THANKS ALOTTTTT!!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.