Django Rest API 类型错误用于位置参数



Django向我触发了这个错误,我不明白为什么。

TypeError at /api/v1/article
__init__() takes 1 positional argument but 2 were given
Request Method:     GET
Request URL:    http://127.0.0.1:8000/api/v1/article
Django Version:     2.2.2
Exception Type:     TypeError
Exception Value:    
__init__() takes 1 positional argument but 2 were given

这是我的序列化程序类:

class ArticleSerializer(serializers.ModelSerializer):
    class Meta:
        model =  Article
        fields = ['id', 'title', 'body', 'category']

这些是我的模型:

from django.db import models
from django.contrib.auth.models import User
class Author(models.Model):
    name = models.ForeignKey(User, on_delete=models.CASCADE)
    detail = models.TextField()
class Category(models.Model):
    name = models.CharField(max_length=100)
class Article(models.Model):
    author = models.ForeignKey(Author, on_delete=models.CASCADE)
    title = models.CharField(max_length=200)
    body = models.TextField()
    category = models.ForeignKey(Category, on_delete=models.CASCADE)

这是我的观点

class ArticleListCreateGet(ListAPIView, CreateAPIView):
    queryset = Article.objects.all()
    serializer_class = ArticleSerializer

这是我的网址

path('api/v1/article', views.ArticleListCreateGet, name='article'),

我的代码没有问题,谁能告诉我为什么我看到上面的错误?

您的路径指的是views.ArticleListCreateGet,这是一个基于类的视图,而不是一个函数。

试着在你的道路上views.ArticleListCreateGet.as_view(),看看会发生什么。

最新更新