django rest api __init__() 采用 1 个位置参数,但给出了 2 个

django rest api __init__() takes 1 positional argument but 2 were given

api link 无效。显示此错误“init() 采用 1 个位置参数,但给出了 2 个”

请帮助我。我是 django 的初学者

正在尝试创建博客api model.py:

class Blog(models.Model):
    blog_id = models.AutoField(primary_key=True, editable=False)
    title = models.CharField(max_length=100, null=False)
    description = models.TextField(null=False)
    image = models.ImageField(null=True, blank=True)
    user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.title

view.py

class BlogViewSet(viewsets.ModelViewSet):
    serializer_class = BlogSerializer
    permission_classes = IsAuthenticated
    queryset = Blog.objects.all()

    def perform_create(self, serializer):
        serializer.save(user=self.request.user)

serializer.py:

class BlogSerializer(serializers.ModelSerializer):
    user = serializers.SerializerMethodField(read_only=True)

    class Meta:
        model = Blog
        fields = '__all__'

urls.py

from django.urls import path
from rest_framework.routers import DefaultRouter

from . import views
from rest_framework_simplejwt.views import (
    TokenObtainPairView,
)

router = DefaultRouter()
router.register('blogs', views.BlogViewSet, basename='blogs')

urlpatterns = [
    path('users/login', views.MyTokenObtainPairView.as_view()),
    path('users/register', views.register_user, name='register'),
    path('users/profile', views.get_user_profile, name='profile'),

    path('blogs/', views.BlogViewSet, name='blogs'),
    path('avatar/', views.get_avatar, name='avatar'),
]

所以当我对“http://localhost:8000/api/blogs/”执行获取请求时,它显示此错误:

TypeError at /api/blogs/

__init__() takes 1 positional argument but 2 were given

Request Method:     GET
Request URL:    http://localhost:8000/api/blogs/
Django Version:     3.2.4
Exception Type:     TypeError
Exception Value:    

__init__() takes 1 positional argument but 2 were given

Exception Location:     D:\work environment\Django_Api\codersavvy\lib\site-packages\django\core\handlers\base.py, line 181, in _get_response
Python Executable:  D:\work environment\Django_Api\codersavvy\Scripts\python.exe
Python Version:     3.9.2
Python Path:    

['D:\work environment\Django_Api\codersavvy',
 'C:\Users\S\AppData\Local\Programs\Python\Python39\python39.zip',
 'C:\Users\S\AppData\Local\Programs\Python\Python39\DLLs',
 'C:\Users\S\AppData\Local\Programs\Python\Python39\lib',
 'C:\Users\S\AppData\Local\Programs\Python\Python39',
 'D:\work environment\Django_Api\codersavvy',
 'D:\work environment\Django_Api\codersavvy\lib\site-packages']

Server time:    Mon, 14 Jun 2021 17:28:12 +0000

您需要调用 .as_view() 以将(class-based)ViewSet 转换为将分派 HTTP 调用的函数:

path('blogs/', views.BlogViewSet<b>.as_view()</b>, name='blogs'),

然而,由于您制作了 View<b>Set</b>,因此您可以将 router [drf-doc] 与:

一起使用
from django.urls import path
from rest_framework.routers import DefaultRouter

from . import views
from rest_framework_simplejwt.views import (
    TokenObtainPairView,
)

<b>router</b> = DefaultRouter()
router.register('blogs', views.BlogViewSet, basename='blogs')

urlpatterns = [
    path('users/login', views.MyTokenObtainPairView.as_view()),
    path('users/register', views.register_user, name='register'),
    path('users/profile', views.get_user_profile, name='profile'),

    path('avatar/', views.get_avatar, name='avatar'),
]

urlpatterns <b>+= router.urls</b>