Я сделал простой API, который перечисляет студентов их уважаемых университетов, используя Django Rest Framework. URL для перечисления данных: http://127.0.0.1:8000/api/v1/students/
. Попробовав API, я получил следующую ошибку
TypeError at /api/v1/students/
'type' object is not iterable
Internal Server Error: /api/v1/students/
Traceback (most recent call last):
File "/Users/gilangramadhanilhami/Desktop/88sparses/shipment/tracking/lib/python3.6/site-packages/django/core/handlers/exception.py", line 35, in inner
response = get_response(request)
File "/Users/gilangramadhanilhami/Desktop/88sparses/shipment/tracking/lib/python3.6/site-packages/django/core/handlers/base.py", line 128, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/Users/gilangramadhanilhami/Desktop/88sparses/shipment/tracking/lib/python3.6/site-packages/django/core/handlers/base.py", line 126, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/gilangramadhanilhami/Desktop/88sparses/shipment/tracking/lib/python3.6/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/Users/gilangramadhanilhami/Desktop/88sparses/shipment/tracking/lib/python3.6/site-packages/django/views/generic/base.py", line 69, in view
return self.dispatch(request, *args, **kwargs)
File "/Users/gilangramadhanilhami/Desktop/88sparses/shipment/tracking/lib/python3.6/site-packages/rest_framework/views.py", line 477, in dispatch
request = self.initialize_request(request, *args, **kwargs)
File "/Users/gilangramadhanilhami/Desktop/88sparses/shipment/tracking/lib/python3.6/site-packages/rest_framework/views.py", line 379, in initialize_request
authenticators=self.get_authenticators(),
File "/Users/gilangramadhanilhami/Desktop/88sparses/shipment/tracking/lib/python3.6/site-packages/rest_framework/views.py", line 273, in get_authenticators
return [auth() for auth in self.authentication_classes]
TypeError: 'type' object is not iterable
Мне удалось найти какое-то решение по StackOverflow, но не так много удачи в поиске решения. Я использую APIView
в файле views.py
и хочу понять, почему это происходит, когда я использовал APIView
(эта проблема также появляется при использовании Viewset
).
Я пытался изменить изменение разрешений, не помогло. Также установка many=True
на «Ложь» также не решила проблему.
У кого-нибудь есть идеи? Заранее спасибо.
Вот другие файлы для лучшего понимания этой проблемы
models.py
from django.db import models
class University(models.Model):
name = models.CharField(max_length=50)
url = models.URLField(unique=True)
class Meta:
verbose_name = "University"
verbose_name_plural = "Universities"
def __str__(self):
return self.name
class Student(models.Model):
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
university = models.ForeignKey(University, on_delete=models.CASCADE,)
class Meta:
verbose_name = "Student"
verbose_name_plural = "Students"
def __str__(self):
return '%s %s' % (self.first_name, self.last_name)
serializers.py
from rest_framework import serializers
from . import models
class StudentSerializer(serializers.ModelSerializer):
class Meta:
fields = (
'id',
'first_name',
'last_name',
'university'
)
model = models.Student
class UniversitySerializer(serializers.ModelSerializer):
class Meta:
fields = (
'id',
'name',
'url'
)
model = models.University
views.py
from rest_framework.views import APIView
from rest_framework.response import Response
from . import models
from . import serializers
class ListStudent(APIView):
def list(self, request, format=None):
students = models.Student.objects.all()
serializer = serializers.StudentSerializer(students, many=True)
return Response(serializer.data)
Студенты / urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.ListStudent.as_view(), name='student_list')
]
urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('api-auth/', include('rest_framework.urls', namespace='rest_framework')),
path('api/v1/students/', include(('students.urls','students'), namespace='students')),
]